I am creating a word document from Excel using VBA and want to insert a Table of Contents at the start of section 2 or on what will be either the 3rd or 4th page depending on what is on the first few pages. The document changes based on many variables. I thought that surely this has been asked and answered many times over but I can't find much and nothing that I can make work.
I used this from another SO post but it only moves to the second page.
wdApp.ActiveDocument.Range(0, 0).InsertBreak Type:=wdSectionBreakNextPage
wdApp.ActiveDocument.TablesOfContents.Add Range:=wdApp.ActiveDocument.Range(0, 0), RightAlignPageNumbers:=True, _
UseHeadingStyles:=True, IncludePageNumbers:=True, UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, UseOutlineLevels:=False
'Insert a page break before existing content
wdApp.ActiveDocument.Range(0, 0).InsertBreak Type:=wdPageBreak
I have a cover page then two/three pages of tables and text then the TOC and then and INTRODUCTION Page. Currently I have a section break before the Intro page thinking I could just set the TOC to go there but all the iterations have put the TOC on page 1 or this one which has put it on page 2.
I tried this which initially put it in the right place at the time but as the rest of the script ran through the TOC slid down the document until it stopped at the end and it ruined all of my formatting.
Selection.HomeKey Unit:=wdStory
With ActiveDocument
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=9, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _True
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
My script is quite long so I have only included the word snippet below but happy to provide more detail if needed. Does anyone know how I can get the table of contents to go exactly where I want it?
With wdApp
.Visible = True
.Activate
.Documents.Open ("C:\Users\v1.dotm")
With .Selection
'Headers
.Sections.First.PageSetup.DifferentFirstPageHeaderFooter = True
'Cover Page
.InsertBreak Type:=7
'Page 2
.BoldRun 'Switch bold on
.TypeText ClientHeading
.BoldRun 'Switch bold off
' Revision Table
RevTable.Copy 'DoEvents
.Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
.InsertBreak Type:=7
'Page 3 and/or 4
.InsertBreak Type:=7
wdApp.ActiveDocument.Range(0, 0).InsertBreak Type:=wdSectionBreakNextPage
wdApp.ActiveDocument.TablesOfContents.Add Range:=wdApp.ActiveDocument.Range(0, 0), RightAlignPageNumbers:=True, _
UseHeadingStyles:=True, IncludePageNumbers:=True, UseHyperlinks:=True, _
HidePageNumbersInWeb:=True, UseOutlineLevels:=False
'Insert a page break before existing content
wdApp.ActiveDocument.Range(0, 0).InsertBreak Type:=wdPageBreak
'Introduction Page
.InsertBreak Type:=7
.Style = wdApp.ActiveDocument.Styles("Heading 1")
.TypeText Text:="INTRODUCTION"
.TypeParagraph
.ParagraphFormat.Alignment = wdAlignParagraphCentre
.Style = wdApp.ActiveDocument.Styles("Body Text")
.TypeText TextToInsert
Note: Below is Word VBA code. Please qualify the object with wdApp
(eg. wdApp.ActiveDocument) if you run it with Excel VBA.
Sub InsertTOC()
Dim doc As Document
Dim rng As Range
Set doc = ActiveDocument
' Check if the document contains at least two sections
If doc.Sections.Count < 2 Then
MsgBox "The document must have at least two sections."
Exit Sub
End If
' Insert a paragraph at the beginning of the section
Set rng = doc.Sections(2).Range
rng.Collapse Direction:=wdCollapseStart
rng.InsertParagraphAfter
rng.Collapse Direction:=wdCollapseEnd
' Insert the table of contents ** code from OP
doc.TablesOfContents.Add Range:=rng, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=9, IncludePageNumbers:=True, AddedStyles:="", _
UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:=True
doc.TablesOfContents(1).TabLeader = wdTabLeaderDots
doc.TablesOfContents.Format = wdIndexIndent
' Update the table of contents
doc.TablesOfContents(1).Update
Set doc = Nothing
Set sec = Nothing
Set rng = Nothing
End Sub
Microsoft documentation: