Log in

View Full Version : [SOLVED:] Modifying Table of Contents with VBA



MajorOrk
11-20-2019, 06:39 AM
I have an Access database that uses VBA to produce a Word document with a Table of Contents. It works fine. Each State is a Heading 1, then locations under them are Heading 2.

Now the customer wants each State in the Table of Contents to start on a new page. I can't figure out how to get VBA to do that. Manually doing an insert page break in the TOC after the document is produced is not an option.

Here is the code I use to produce the TOC from Access and a picture of what the customer wants.

Thanks,


Set myWordRange = WordApp.ActiveDocument.Range(0, 0)
' code to insert text "TABLE OF CONTENTS" above the actual TOC table
WordApp.ActiveDocument.TablesOfContents.Add myWordRange, _
UseFields:=False, _
UseHeadingStyles:=True, _
LowerHeadingLevel:=3, _
UpperHeadingLevel:=1, _
UseHyperlinks:=True
With WordApp.ActiveDocument
.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
--------------------------------------------------

25444

Paul_Hossler
11-20-2019, 09:06 AM
In the Word document, change the TOC1 style paragraph format to "Page Break Before"


25445

MajorOrk
11-20-2019, 09:35 AM
Paul, thanks. I got that and can do it after the document is produced from Access (it's over 1,200 pages by the way).

I was just looking for a way to have VBA do it when it creates the TOC on the fly. I'll have to read up on creating a modified TOC style with VBA.

Again, thanks,

Mavila
11-20-2019, 10:06 AM
I think each line of the TOC can be considered a paragraph, so you could do a "For Each" loop through each line and insert the page break as needed.

After the TOC is created with VBA, try this:



Dim myPar As Paragraph
For Each myPar In ActiveDocument.Paragraphs
myPar.Range.Select
If Selection Like "*" & Chr(9) & "*" And Selection.Paragraphs.Count = 1 Then 'Tests for selection being in TOC and having a single line in the TOC selected
Selection.End = Selection.Start
Selection.InsertBreak Type:=7
End If
Next


You'll probably also have to test the selection to confirm it's a state name. You could do that perhaps with font properties, or you could store the names of states in an array and then loop through the array.

MajorOrk
11-20-2019, 10:51 AM
Turns out it was a lot simpler than I thought! I just recorded a macro of me doing Paul's recommendation and then looked at the VB. All I had to do was insert the following after my Access VB TOC generating code:


With WordApp.ActiveDocument.Styles("TOC 1").ParagraphFormat
.PageBreakBefore = True
End With

Thanks for the help.

Paul_Hossler
11-20-2019, 12:42 PM
A more general way (and IMHO ... better) would be that a Word DOTX with the styles, formatting, etc. and create a DOCXwith Access from that


This is sort of a blind shot, since I don't know how you're using Access to make the Word document, assign styles, etc.