0
votes

I am building a word document using Office's Word interop. I am settings heading styles via code. The content is getting styled correctly, however, one opening the created word document, the style is not selected for the selection in the header. This is causing the table of contents to not find the heading.

object oMissing = Missing.Value;

//Start Word and create a new document.
var application = new Application();
application.Visible = true;
var document = application.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

foreach (var member in assembly.Members)
{
    //Insert a paragraph at the beginning of the document
    var paragraph = document.Content.Paragraphs.Add(ref oMissing);
    paragraph.set_Style(WdBuiltinStyle.wdStyleHeading1);
    paragraph.Range.Text = member.MemberName;
    paragraph.Range.InsertParagraphAfter();
}

document.TablesOfContents.Add(document.Content, true /*use heading styles*/, oMissing, oMissing, oMissing,
                                                    oMissing, oMissing, oMissing, oMissing, oMissing,
                                                    oMissing, oMissing);

document.SaveAs(@"C:\test.docx", oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
document.Close();
application.Quit();

This example causes the table of contents to display "No entries found".

Note that the content is correctly styled with the heading style. However, when I open the document manually and select "Heading 1", then the table of contents finds the entry correctly.

Any ideas why this would happen? I am callign set_Style with a built in style name. Why does it apply the style but not actually get treated as the style (in this case, a heading)?

UPDATE

Adding the following code seems to make only the first heading recognized as a heading in Word.

    foreach (var member in assembly.Members)
    {
        document.ActiveWindow.Selection.set_Style(WdBuiltinStyle.wdStyleHeading1);
        //Insert a paragraph at the beginning of the document
        var paragraph = document.Content.Paragraphs.Add(ref oMissing);
        paragraph.set_Style(WdBuiltinStyle.wdStyleHeading1);
        paragraph.Range.Text = member.MemberName;
        paragraph.Range.InsertParagraphAfter();
    }
3

3 Answers

1
votes

I have noticed difference in the order of the operations

Follow code will not generate real header, it is only looks like header

/*1*/ paragraph.set_Style(WdBuiltinStyle.wdStyleHeading1);
/*2*/ paragraph.Range.Text = "Head";
/*3*/ paragraph.Range.InsertParagraphAfter();

To solve propblem you should use this code

/*1*/ paragraph.Range.Text = "Head";
/*2*/ paragraph.set_Style(WdBuiltinStyle.wdStyleHeading1);
/*3*/ paragraph.Range.InsertParagraphAfter();
0
votes

After Adding the Paragraph select the paragraph it might work .

paragraph.Range().Select()
0
votes

I had to do something similar, but the paragraph was not getting the style...

I used then TypeText instead, and then worked...I used a Word.Selection too. I´m merging documents, but I guess your code should be something like this:

(after the line: var document = application.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing)).I´m using Word = Microsoft.Office.Interop.Word on the usings, so that "Word" before the selection is realated to that.

Word.Selection selection = application.Selection;

foreach (var member in assembly.Members)
{
  selection.TypeText(member.MerberName);
  selection.set_Style(WdBuiltinStyle.wdStyleHeading1);
  selection.TypeParagraph();
}

That worked here and the TOC is getting filled.