Hi, I am trying to Automate the Word document using vb.net
I am facing problems when i try to define a section, insert section breaks and getting it updated in the table of contents.
I will break down the steps that I am trying to do:
1) Add Multiple sections to word document.
2) Define the Section Heading.
3) Add the text that I want to put under this section.
4) Insert Section Break
5) Define another section, follow steps 2,3,4
6) Update all the Section headings and page numbers in the Table of Contents in my first Page.
Problems I am facing:
1) I cannot add the text under the specific section
2) when there is a Page break or Section break the numbers are not getting updated properly (See the Testing section as numbered below)
For Eg: My Table of Contents/ Sections are numbered in the document as below
1) Introduction
2) Electrical Parts
3) Mechanical Parts
1) Testing
3) I have added a table under section Testing and issue is all the cells inside the table are getting numbered.
4) How to define sub-sections numbered in a sequence
Dim oWord As Word.Application Dim oDoc As Word.Document Dim oTable As Word.Table 'Start Word and open the document template. oWord = CreateObject("Word.Application") oWord.Visible = True oDoc = oWord.Documents.Add Dim oRng As Word.Range = oDoc.Content
In the code below: - I cannot get the text added under the section (oRng.Text is not adding any text to the doc)
'-----------------Define Section-------------------------------------------------- oRng.Text = "Introduction" oRng.Style = Word.WdBuiltinStyle.wdStyleHeading1 oRng.ListFormat.ApplyNumberDefault() oRng.InsertAfter(vbCr) oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd) oRng.Style = Word.WdBuiltinStyle.wdStyleNormal oRng.Style = Word.WdBuiltinStyle.wdStyleNoteHeading oRng.Text = "This document is to define the specification for Electronic Parts" oRng.InsertBreak(Word.WdBreakType.wdSectionBreakContinuous) oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
'--------------Insert Table of Contents--------------------------------------------------------------- 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 'Page Break after Table of Contents oWord.Selection.InsertBreak() 'Update Table of Contents oDoc.TablesOfContents(1).Update() oDoc.TablesOfContents(1).UpdatePageNumbers()
This is how it is seen in the document: TOC is not getting updated properly
1..................................................................................................1
2 Introduction..............................................................................2
3 Electrical Parts..........................................................................3
4 Mechanical Parts.......................................................................4
1 Testing......................................................................................5
Any help on this to get it sorted out would be great and much appreciated.