Kanjoo
Kanjoo

Reputation: 111

Bold a part of string/range in each line using Microsoft.Office.Interop.Word

I have seen few codes to bold a part of range but those are specific to those examples. In my case I am writing datagridview values to word using Microsoft.Office.Interop.Word. I want to bold a specific value to be bold/italic. I am using following code.

        Microsoft.Office.Interop.Word.Application objword = new Microsoft.Office.Interop.Word.Application();
        objword.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateNormal;
        Microsoft.Office.Interop.Word.Document objDoc = objword.Documents.Add();

        Microsoft.Office.Interop.Word.Paragraph para1;
        para1 = objDoc.Paragraphs.Add();
        String text = "";
        for (int r = 0; r < dgvlib.RowCount; r++)
        {
            text = text + dgvlib.Rows[r].Cells[1].Value.ToString();
            if (dgvlib.Rows[r].Cells[11].Value.ToString()!="")
                text = text +  " Comments:" + dgvlib.Rows[r].Cells[11].Value.ToString() + " ";
            if (dgvlib.Rows[r].Cells[10].Value.ToString() != "")
                text = text +  " ( Bold Text:" + dgvlib.Rows[r].Cells[10].Value.ToString() + ")";
            text = text + "\n";
        }
        para1.Range.Font.Size = 9;
        para1.Range.Font.Name = "Arial";
        para1.Range.Text = text;
        para1.Range.Paragraphs.Add();
        objDoc.SaveAs2(fNameExportWord);
        objword.Visible = true;

I want to bold only this text dgvlib.Rows[r].Cells[10].Value.ToString() (second if condition). If I use different ranges or para then it creates new paragraphs.

In fact datagrid view dgblib rows are being written line by line so in each new line the value to be bold is dynamic. Sample to be written is below (if rows are 3).

this is row to be written to the word. Comments: these are comments. (Bold Text: 456)

this is row2 to be written to the word. Comments: these are comments. (Bold Text: 789)

this is row3 to be written to the word. Comments: these are comments. (Bold Text: 123)

Upvotes: 0

Views: 2666

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

As always, there's more than one way to solve a problem with text insertion and formatting. One way would be to store or mark the values that need to be formatted differently and, after inserting them, use Find to locate them and apply that formatting.

Another way, that the sample code below demonstrates, is basically what the code in the question is trying to do: format as the text is inserted. This does not allow inserting the entire text as a string, however. The text needs to be broken down: each section requiring different formatting needs to be inserted individually.

In order to do this, it's necessary to work with Range objects. The sample code uses two Range objects: one for the entire new content and one for inserting the sections of text. When appending new content to a Range, as long as nothing needs to be done to it, the InsertAfter method can be used.

As soon as the new content needs to be manipulated in some way it's necessary to first "collapse" the Range to a "point", then append the new content. At this point, the Rangecontains only the new content so any formatting applied affects only the new content.

In the sample code I've tried to stay as close to the original as possible in order to make it easier to follow and understand in relation to the original - so it's not optimized...

    Microsoft.Office.Interop.Word.Application objword = new Microsoft.Office.Interop.Word.Application();
    objword.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateNormal;
    Microsoft.Office.Interop.Word.Document objDoc = objword.Documents.Add();

    Microsoft.Office.Interop.Word.Range rngFull = objDoc.Content;
    Microsoft.Office.Interop.Word.Range rngTarget = rngFull.Duplicate;
    rngTarget.InsertAfter("\n");
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rngTarget.Collapse(ref oCollapseEnd);

    String text = "";
    for (int r = 0; r < dgvlib.RowCount; r++)
    {
        text = text + dgvlib.Rows[r].Cells[1].Value.ToString();
        if (dgvlib.Rows[r].Cells[11].Value.ToString()!="")
        {
            text = text +  " Comments:" + dgvlib.Rows[r].Cells[11].Value.ToString() + " ";
            rngTarget.InsertAfter(text);
            text = "";
            rngTarget.Collapse(ref oCollapseEnd);
        }
        else if (dgvlib.Rows[r].Cells[10].Value.ToString() != "")
        {
            text = text +  " ( Bold Text:";
            rngTarget.InsertAfter(text);
            text = "";
            rngTarget.Collapse(ref oCollapseEnd);
            rngTarget.Text = dgvlib.Rows[r].Cells[10].Value.ToString();
            rngTarget.Font.Bold = -1;
            rngTarget.Collapse(ref oCollapseEnd);
            rngTarget.InsertAfter(")");
            rngTarget.Font.Bold = 0;
        }
        text = text + "\n";
    }
    rngFull.Font.Size = 9;
    rngFull.Font.Name = "Arial";
    //para1.Range.Text = text;
    //rngFull.Paragraphs.Add();
    objDoc.SaveAs2(fNameExportWord);
    objword.Visible = true;

Upvotes: 3

Related Questions