none
Adding Section Headings and updating TOC in Word using vb.net RRS feed

  • Question

  • I am trying to Automate the Word document using VB.net

    I am trying to find an example that can show me how to update the word document with Sections and Section headings. Once the sections are created it should be updated in Table of Contents. (As simple as that).

    But the problem is I am not sure of the syntax to be used associated with Sections.

    I have done the following using vb.net

    1) I have created the Word document 2) Added some Paragraphs 3) Created some Tables & Figures Charts,...blah..blah

    What I am trying to do now:

    • Define Sections
    • Add Section Heading
    • Update the Headings created in the Table of Contents before closing the Document.

    i.e,. For Example

    1. Introduction

    1.1 Mechanical

    1.2 Electrical

    2. Test Conditions

    Update this in TOC as

    1. Introduction .... 1

    1.1 Mechanical .... 2

    1.2 Electrical ..... 4

    2. Test Conditions


    I already know how to update the TOC as mentioned below:

    With oDoc
                .TablesOfContents.Add(Range:=oWord.Selection.Range, _
                           RightAlignPageNumbers:=True, _
                           UseHeadingStyles:=True, _
                           IncludePageNumbers:=True, _
                           AddedStyles:="styleSection", _
                           UseHyperlinks:=False, _
                           HidePageNumbersInWeb:=True, _
                           UseOutlineLevels:=True)
                .TablesOfContents(1).Range.Font.Name = "Arial Narrow"
                .TablesOfContents(1).Range.Font.Size = 11
                .TablesOfContents(1).TabLeader = Word.WdTabLeader.wdTabLeaderDots
                .TablesOfContents.Format = Word.WdTocFormat.wdTOCSimple
    
    End With
    
            oDoc.TablesOfContents(1).Update()
            oDoc.TablesOfContents(1).UpdatePageNumbers()

    But all I am trying to find now is how to add Sections and add text as section headings ?! I did lot of search on this but not able to find the right solution.

    The Below code doesn't seems to work ->

    Dim oWord As Word.Application
    Dim oSection As Word.Section
    
    oSection = oDoc.Sections.Add
    oSection.Range.Text = "INTRODUCTION"
    oSection.Range.InsertAfter("This is Section 1")

    Tuesday, April 29, 2014 1:59 PM

Answers

  • Hi Uday

    I think, in Word terms, what you need is to use HEADING STYLES. In Word, "Section" is a special technical term for a structure that allows you to define different properties, such as margins, page orientation, page numbering, headers and footers for part of a document. It has nothing directly to do with a Table of Contents (TOC).

    By default, a TOC will base on the first three built-in Heading styles (Heading 1, Heading 2, Heading 3). The TOC can be defined differently, without these, but that is the default.

    So what you'd need to do is apply the appropriate Heading style to the paragraphs that should appear in the TOC.

    Assuming you're creating the document from scratch, I'd change your last code snippet to something more like:

    Dim oWord as Word.App.ication
    Dim oDoc as Word.Document

    'get the application, get the document, then
    Dim rngDoc as Word.Range = oDoc.Content
    rngDoc.Text = "Introduction"
    rngDoc.Style = Word.WdBuiltinStyle.wdStyleHeading1 ' or "Heading 1"
    rngDoc.InsertAfter vbCr
    'Go to the end of the range
    rngDoc.Collapse Word.WdCollapseDirection.wdCollapseEnd
    rngDoc.Style = Word.WdBuiltinStyle.wdStyleNormal
    rngDoc.Text = "This is text under Heading 1"


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Uday22 Thursday, May 1, 2014 10:26 AM
    Tuesday, April 29, 2014 5:05 PM
    Moderator
  • Hi,

    According to your description, I think you want to add new sections and update the Table of Content to contain the heading of these new sections. In fact, the code above will add a new section in Word document succesfully, but the section heading won't be inserted into TOC since you don't set the style of the range as heading.

    Here is a sample for your reference. You could refer to it to set the heading style as you want and then update the the TOC.

    Dim oDoc As Word.Document
    Dim oSection As Word.Section
    
    oSection = oDoc.Sections.Add
    
    oSection.Range.Text = "INTRODUCTION"
    oSection.Range.Style = oDoc.Styles("Heading 1")
    
    oDoc.TablesOfContents(1).Update()
    oDoc.TablesOfContents(1).UpdatePageNumbers()
    




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Uday22 Thursday, May 1, 2014 10:30 AM
    Wednesday, April 30, 2014 8:59 AM
    Moderator

All replies

  • I am trying to Automate the Word document using VB.net

    I am trying to find an example that can show me how to update the word document with Sections and Section headings. Once the sections are created it should be updated in Table of Contents. (As simple as that).

    But the problem is I am not sure of the syntax to be used associated with Sections.

    I have done the following using vb.net

    1) I have created the Word document 2) Added some Paragraphs 3) Created some Tables & Figures Charts,...blah..blah

    What I am trying to do now:

    • Define Sections
    • Add Section Heading
    • Update the Headings created in the Table of Contents before closing the Document.

    i.e,. For Example

    1. Introduction

    1.1 Mechanical

    1.2 Electrical

    2. Test Conditions

    Update this in TOC as

    1. Introduction .... 1

    1.1 Mechanical .... 2

    1.2 Electrical ..... 4

    2. Test Conditions


    I already know how to update the TOC as mentioned below:

    With oDoc
                .TablesOfContents.Add(Range:=oWord.Selection.Range, _
                           RightAlignPageNumbers:=True, _
                           UseHeadingStyles:=True, _
                           IncludePageNumbers:=True, _
                           AddedStyles:="styleSection", _
                           UseHyperlinks:=False, _
                           HidePageNumbersInWeb:=True, _
                           UseOutlineLevels:=True)
                .TablesOfContents(1).Range.Font.Name = "Arial Narrow"
                .TablesOfContents(1).Range.Font.Size = 11
                .TablesOfContents(1).TabLeader = Word.WdTabLeader.wdTabLeaderDots
                .TablesOfContents.Format = Word.WdTocFormat.wdTOCSimple
    
    End With
    
            oDoc.TablesOfContents(1).Update()
            oDoc.TablesOfContents(1).UpdatePageNumbers()

    But all I am trying to find now is how to add Sections and add text as section headings ?! I did lot of search on this but not able to find the right solution.

    The Below code doesn't seems to work ->

    Dim oWord As Word.Application
    Dim oSection As Word.Section
    
    oSection = oDoc.Sections.Add
    oSection.Range.Text = "INTRODUCTION"
    oSection.Range.InsertAfter("This is Section 1")
    Tuesday, April 29, 2014 11:48 AM
  • Have you tried using the macro recorder to generate the code? I've found that usually gives me a good place to start.

    You might want to try posting to the Word for Developers forum as well.

    http://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, April 29, 2014 12:24 PM
  • Hi Uday

    I think, in Word terms, what you need is to use HEADING STYLES. In Word, "Section" is a special technical term for a structure that allows you to define different properties, such as margins, page orientation, page numbering, headers and footers for part of a document. It has nothing directly to do with a Table of Contents (TOC).

    By default, a TOC will base on the first three built-in Heading styles (Heading 1, Heading 2, Heading 3). The TOC can be defined differently, without these, but that is the default.

    So what you'd need to do is apply the appropriate Heading style to the paragraphs that should appear in the TOC.

    Assuming you're creating the document from scratch, I'd change your last code snippet to something more like:

    Dim oWord as Word.App.ication
    Dim oDoc as Word.Document

    'get the application, get the document, then
    Dim rngDoc as Word.Range = oDoc.Content
    rngDoc.Text = "Introduction"
    rngDoc.Style = Word.WdBuiltinStyle.wdStyleHeading1 ' or "Heading 1"
    rngDoc.InsertAfter vbCr
    'Go to the end of the range
    rngDoc.Collapse Word.WdCollapseDirection.wdCollapseEnd
    rngDoc.Style = Word.WdBuiltinStyle.wdStyleNormal
    rngDoc.Text = "This is text under Heading 1"


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Uday22 Thursday, May 1, 2014 10:26 AM
    Tuesday, April 29, 2014 5:05 PM
    Moderator
  • Hi,

    According to your description, I think you want to add new sections and update the Table of Content to contain the heading of these new sections. In fact, the code above will add a new section in Word document succesfully, but the section heading won't be inserted into TOC since you don't set the style of the range as heading.

    Here is a sample for your reference. You could refer to it to set the heading style as you want and then update the the TOC.

    Dim oDoc As Word.Document
    Dim oSection As Word.Section
    
    oSection = oDoc.Sections.Add
    
    oSection.Range.Text = "INTRODUCTION"
    oSection.Range.Style = oDoc.Styles("Heading 1")
    
    oDoc.TablesOfContents(1).Update()
    oDoc.TablesOfContents(1).UpdatePageNumbers()
    




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Uday22 Thursday, May 1, 2014 10:30 AM
    Wednesday, April 30, 2014 8:59 AM
    Moderator
  • Thanks Cindy.

    That was a good explanation. 

    It works perfect now.

    Thursday, May 1, 2014 10:28 AM
  • Thanks Luna for the explanation. 

    I got this sorted out now and works fine.

    Thursday, May 1, 2014 10:31 AM
  • Hi there I'm also having these issues on a similar project.

    I have managed to get a little success but I'm getting errors once I try modifying above the 1st level of heading.

    Here is my code snippet

    Dim iH_1 as Uint16 = 0
    Dim iH_2 as Uint16 = 0
    Dim iH_3 as Uint16 = 0
    Dim iH_4 as Uint16 = 0
    
    For each sec as section in oDoc.Sections
    For each par as paragraph in oDoc.Sections(sec.index).Range.Paragraphs
    Select case par.style.nameLocal
    case "Heading 1"
    iH_1 += 1
    iH_2 = 0
    iH_3 = 0
    iH_4 = 0
    par.Range.Text = iH_1 & ". "
    
    case "Heading 2"
    iH_2 += 1
    iH_3 = 0
    iH_4 = 0
    par.Range.Text = iH_1 & ". " & iH_2 & ". "
    
    case "Heading 3"
    iH_3 += 1
    iH_4 = 0
    par.Range.Text = iH_1 & ". " & iH_2 & ". "& iH_3 & ". "
    
    case "Heading 4"
    iH_4 += 1
    par.Range.Text = iH_1 & ". " & iH_2 & ". "& iH_3 & ". "& iH_4 & ". "
    
    End Select
    Next par
    Next sec

    which seems to be working if I comment out Headings 2 to 4 and only process Heading 1

    Tuesday, June 21, 2016 3:21 PM