none
How do I create a Table of Contents for specific heading levels in Word 2016 VSTO code? RRS feed

  • Question

  • Users would like to have a TOC looking like this

    Contents

    1.      MAIN HEADING 1............................................................................................................ 1

                       Sub heading 1.1 .................................................................................................... 1

                       Sub heading 1.2 .................................................................................................... 1

    1.      MAIN HEADING 2............................................................................................................ 1

                       Sub heading 2.1...................................................................................................... 1

                       Sub heading 2.1...................................................................................................... 1

    for the following text:

    1.               MAIN HEADING 1

    1.1             Sub paragraph 1.1

                  Sub heading 1.1

    1.2             Sub paragraph 1.2

                  Sub heading 1.2

    2.               MAIN HEADING 2

    2.1             Sub paragraph 2.1

                  Sub heading 2.1

    2.2             Sub paragraph 2.2

                  Sub heading 2.2

    Effectively, users would like the second heading level to be missed in TOC. Is there any way to do this by tweaking ActiveDocument.TablesOfContents.Add() method parameters? The add-in code that I am working with was written for Word 2007 and TOC was generated as users requested. However it does not do the same TOC for Word 2016. The Word 2007 is as follows

    Word.Style style = _dasDocument.GetStyle("TOC 1");
                    style.AutomaticallyUpdate = false;
                    object m1;
                    m1 = "Normal";
                    style.set_BaseStyle(ref m1);
                    style.set_NextParagraphStyle(ref m1);
                    Word.ParagraphFormat paragraphFormat = style.ParagraphFormat;
                    paragraphFormat.LeftIndent = _dasDocument.Application.CentimetersToPoints((float)0.7);
                    paragraphFormat.RightIndent = _dasDocument.Application.CentimetersToPoints(0);
                    paragraphFormat.SpaceBefore = 6;
                    paragraphFormat.SpaceAfter = 0;
                    paragraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
                    paragraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                    paragraphFormat.WidowControl = -1;
                    paragraphFormat.KeepWithNext = 0;
                    paragraphFormat.KeepTogether = 0;
                    paragraphFormat.PageBreakBefore = 0;
                    paragraphFormat.NoLineNumber = 0;
                    paragraphFormat.Hyphenation = -1;
                    paragraphFormat.FirstLineIndent = _dasDocument.Application.CentimetersToPoints((float)-0.7);
                    paragraphFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
     
                    Word.Font font = style.Font;
                    font.Name = Constants.GetPcFontName(_dasDocument.Application);
                    font.Size = (float)10.5;
                    font.Bold = 0;
                    font.Italic = 0;
                    font.Underline = Word.WdUnderline.wdUnderlineNone;
                    font.StrikeThrough = 0;
                    font.DoubleStrikeThrough = 0;
                    font.Outline = 0;
                    font.Emboss = 0;
                    font.Shadow = 0;
                    font.Hidden = 0;
                    font.SmallCaps = 0;
                    font.AllCaps = -1;
                    font.ColorIndex = Word.WdColorIndex.wdAuto;
                    font.Engrave = 0;
                    font.Superscript = 0;
                    font.Subscript = 0;
                    font.Scaling = 100;
                    font.Kerning = 0;
                    font.Animation = Word.WdAnimation.wdAnimationNone;
                    object m2 = -1, m3 = 1, m4 = 1, m5 = System.Reflection.Missing.Value, m6 = System.Reflection.Missing.Value;
                    object m7 = -1, m8 = -1, m9 = "Sub heading,2,Schedule,1,Schedule Name,2,Execution,1";
                    object m10 = System.Reflection.Missing.Value, m11 = System.Reflection.Missing.Value, m12 = System.Reflection.Missing.Value;
                    _dasDocument.ActiveDocument.TablesOfContents.Add(Selection.Range, ref m2, ref m3, ref m4, ref m5, ref m6, ref m7, ref m8, ref m9, ref m10, ref m11, ref m12);
                    _dasDocument.ActiveDocument.TablesOfContents[1].TabLeader = Microsoft.Office.Interop.Word.WdTabLeader.wdTabLeaderDots;

    Thanks

    VVS


    B7

    Tuesday, February 7, 2017 8:23 PM

Answers

  • Hi,

    What is your _dasDocument, Constants?

    Using code below, it could successfully generate TOC in Office2016. But it does work in Office2007.

    Since I have done some changes to let your code work for me, I suggest you share us a sample project for testing.

    Word.Application _dasDocument = Globals.ThisAddIn.Application.Documents.Application;
                Word.Style style = _dasDocument.ActiveDocument.Styles["TOC 1"];
                Word.Selection selection = Globals.ThisAddIn.Application.Documents.Application.Selection;
                Word.Section section = selection.Sections[1];
                style.AutomaticallyUpdate = false;
                object m1;
                m1 = "Normal";
                style.set_BaseStyle(ref m1);
                style.set_NextParagraphStyle(ref m1);
                Word.ParagraphFormat paragraphFormat = style.ParagraphFormat;
                paragraphFormat.LeftIndent = _dasDocument.Application.CentimetersToPoints((float)0.7);
                paragraphFormat.RightIndent = _dasDocument.Application.CentimetersToPoints(o);
                paragraphFormat.SpaceBefore = 0;
                paragraphFormat.SpaceAfter = 0;
                paragraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
                paragraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                paragraphFormat.WidowControl = -1;
                paragraphFormat.KeepWithNext = 0;
                paragraphFormat.KeepTogether = 0;
                paragraphFormat.PageBreakBefore = 0;
                paragraphFormat.NoLineNumber = 0;
                paragraphFormat.Hyphenation = -1;
                paragraphFormat.FirstLineIndent = _dasDocument.Application.CentimetersToPoints((float)-0.7);
                paragraphFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
    
                Word.Font font = style.Font;
                //font.Name = Constants.GetPcFontName(_dasDocument.Application);
                font.Size = (float)10.5;
                font.Bold = 0;
                font.Italic = 0;
                font.Underline = Word.WdUnderline.wdUnderlineNone;
                font.StrikeThrough = 0;
                font.DoubleStrikeThrough = 0;
                font.Outline = 0;
                font.Emboss = 0;
                font.Shadow = 0;
                font.Hidden = 0;
                font.SmallCaps = 0;
                font.AllCaps = -1;
                font.ColorIndex = Word.WdColorIndex.wdAuto;
                font.Engrave = 0;
                font.Superscript = 0;
                font.Subscript = 0;
                font.Scaling = 100;
                font.Kerning = 0;
                font.Animation = Word.WdAnimation.wdAnimationNone;
                object m2 = -1, m3 = 1, m4 = 2, m5 = System.Reflection.Missing.Value, m6 = System.Reflection.Missing.Value;
                object m7 = -1, m8 = -1, m9 = "Sub heading,2,Schedule,1,Schedule Name,2,Execution,1";
                object m10 = System.Reflection.Missing.Value, m11 = System.Reflection.Missing.Value, m12 = System.Reflection.Missing.Value;
                _dasDocument.ActiveDocument.TablesOfContents.Add(selection.Range, ref m2, ref m3, ref m4, ref m5, ref m6, ref m7, ref m8, ref m9, ref m10, ref m11, ref m12);
                _dasDocument.ActiveDocument.TablesOfContents[1].TabLeader = Microsoft.Office.Interop.Word.WdTabLeader.wdTabLeaderDots;
    
    
                Word.HeaderFooter headerFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                headerFooter.PageNumbers.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleLowercaseRoman;
                m1 = Word.WdBreakType.wdSectionBreakNextPage;
                selection.InsertBreak(ref m1);
    
                section = selection.Sections[1];
                headerFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                headerFooter.PageNumbers.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleArabic;
    
                //Insert and format a TOC heading
                _dasDocument.ActiveDocument.Sections[selection.Sections[1].Index - 1].Range.Select();
                m1 = Word.WdCollapseDirection.wdCollapseStart;
                selection.Collapse(ref m1);
                selection.InsertAfter("Content\r");
                m1 = Word.WdBuiltinStyle.wdStyleTOC1;
                selection.set_Style(ref m1);
                selection.ParagraphFormat.SpaceBefore = (float)0;
                selection.ParagraphFormat.SpaceAfter = (float)12;
                selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                selection.Font.Bold = -1;
                m1 = Word.WdCollapseDirection.wdCollapseStart;
                selection.Collapse(ref m1);

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Bloke No 7 Monday, February 20, 2017 2:14 AM
    Wednesday, February 8, 2017 8:35 AM
    Moderator

All replies

  • I've just noticed the code in my program that formats the TOC generated above:

    Word.Section section = Selection.Sections[1];
          Word.HeaderFooter headerFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
          headerFooter.PageNumbers.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleLowercaseRoman;
          object m1 = Word.WdBreakType.wdSectionBreakNextPage;
          Selection.InsertBreak(ref m1);
     
          section = Selection.Sections[1];
          headerFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
          headerFooter.PageNumbers.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleArabic;
     
          //Insert and format a TOC heading
          _dasDocument.ActiveDocument.Sections[Selection.Sections[1].Index - 1].Range.Select();
          m1 = Word.WdCollapseDirection.wdCollapseStart;
          Selection.Collapse(ref m1);
          Selection.InsertAfter("Contents\r");
          m1 = Word.WdBuiltinStyle.wdStyleTOC1;
          Selection.set_Style(ref m1);
          Selection.ParagraphFormat.SpaceBefore = (float)0;
          Selection.ParagraphFormat.SpaceAfter = (float)12;
          Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
          Selection.Font.Bold = -1;
          m1 = Word.WdCollapseDirection.wdCollapseStart;
          Selection.Collapse(ref m1);

    Apparently, collapsing a specific document sections does the TOC formatting work hiding second level headings. Why does it not work for Word 2016? Thanks


    B7




    • Edited by Bloke No 7 Tuesday, February 7, 2017 8:54 PM
    Tuesday, February 7, 2017 8:35 PM
  • Hi,

    What is your _dasDocument, Constants?

    Using code below, it could successfully generate TOC in Office2016. But it does work in Office2007.

    Since I have done some changes to let your code work for me, I suggest you share us a sample project for testing.

    Word.Application _dasDocument = Globals.ThisAddIn.Application.Documents.Application;
                Word.Style style = _dasDocument.ActiveDocument.Styles["TOC 1"];
                Word.Selection selection = Globals.ThisAddIn.Application.Documents.Application.Selection;
                Word.Section section = selection.Sections[1];
                style.AutomaticallyUpdate = false;
                object m1;
                m1 = "Normal";
                style.set_BaseStyle(ref m1);
                style.set_NextParagraphStyle(ref m1);
                Word.ParagraphFormat paragraphFormat = style.ParagraphFormat;
                paragraphFormat.LeftIndent = _dasDocument.Application.CentimetersToPoints((float)0.7);
                paragraphFormat.RightIndent = _dasDocument.Application.CentimetersToPoints(o);
                paragraphFormat.SpaceBefore = 0;
                paragraphFormat.SpaceAfter = 0;
                paragraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
                paragraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                paragraphFormat.WidowControl = -1;
                paragraphFormat.KeepWithNext = 0;
                paragraphFormat.KeepTogether = 0;
                paragraphFormat.PageBreakBefore = 0;
                paragraphFormat.NoLineNumber = 0;
                paragraphFormat.Hyphenation = -1;
                paragraphFormat.FirstLineIndent = _dasDocument.Application.CentimetersToPoints((float)-0.7);
                paragraphFormat.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;
    
                Word.Font font = style.Font;
                //font.Name = Constants.GetPcFontName(_dasDocument.Application);
                font.Size = (float)10.5;
                font.Bold = 0;
                font.Italic = 0;
                font.Underline = Word.WdUnderline.wdUnderlineNone;
                font.StrikeThrough = 0;
                font.DoubleStrikeThrough = 0;
                font.Outline = 0;
                font.Emboss = 0;
                font.Shadow = 0;
                font.Hidden = 0;
                font.SmallCaps = 0;
                font.AllCaps = -1;
                font.ColorIndex = Word.WdColorIndex.wdAuto;
                font.Engrave = 0;
                font.Superscript = 0;
                font.Subscript = 0;
                font.Scaling = 100;
                font.Kerning = 0;
                font.Animation = Word.WdAnimation.wdAnimationNone;
                object m2 = -1, m3 = 1, m4 = 2, m5 = System.Reflection.Missing.Value, m6 = System.Reflection.Missing.Value;
                object m7 = -1, m8 = -1, m9 = "Sub heading,2,Schedule,1,Schedule Name,2,Execution,1";
                object m10 = System.Reflection.Missing.Value, m11 = System.Reflection.Missing.Value, m12 = System.Reflection.Missing.Value;
                _dasDocument.ActiveDocument.TablesOfContents.Add(selection.Range, ref m2, ref m3, ref m4, ref m5, ref m6, ref m7, ref m8, ref m9, ref m10, ref m11, ref m12);
                _dasDocument.ActiveDocument.TablesOfContents[1].TabLeader = Microsoft.Office.Interop.Word.WdTabLeader.wdTabLeaderDots;
    
    
                Word.HeaderFooter headerFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                headerFooter.PageNumbers.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleLowercaseRoman;
                m1 = Word.WdBreakType.wdSectionBreakNextPage;
                selection.InsertBreak(ref m1);
    
                section = selection.Sections[1];
                headerFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                headerFooter.PageNumbers.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleArabic;
    
                //Insert and format a TOC heading
                _dasDocument.ActiveDocument.Sections[selection.Sections[1].Index - 1].Range.Select();
                m1 = Word.WdCollapseDirection.wdCollapseStart;
                selection.Collapse(ref m1);
                selection.InsertAfter("Content\r");
                m1 = Word.WdBuiltinStyle.wdStyleTOC1;
                selection.set_Style(ref m1);
                selection.ParagraphFormat.SpaceBefore = (float)0;
                selection.ParagraphFormat.SpaceAfter = (float)12;
                selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                selection.Font.Bold = -1;
                m1 = Word.WdCollapseDirection.wdCollapseStart;
                selection.Collapse(ref m1);

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Bloke No 7 Monday, February 20, 2017 2:14 AM
    Wednesday, February 8, 2017 8:35 AM
    Moderator