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
- Introduction
1.1 Mechanical
1.2 Electrical
2. Test Conditions
Update this in TOC as
- 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")