Log in

View Full Version : [SOLVED:] Tables of Contents and Using .Find with a Do While .Execute = True



Mavila
06-22-2022, 05:09 PM
I'm working with a large document with multiple TOCs (each chapter has a TOC). I was looping through all of the TOCs after the document was constructed to update the field code and apply formatting because of some strange behavior.

After some trial and error, I thought that maybe the most efficient way to do it would be to find the TOC after the chapter is inserted (rather than loop through them all at the end and do Selection.MoveRight/Left/Down,etc.).

I can find the TOC by looping through the fields in the chapter and then select it. I find each tab and format it with a dotted underline (I couldn't consistent results other ways which is one of the strange behaviors I was experiencing).

I do a Find vbtab and then a Do While .Execute = True and format the underline. It works great but it does the loop twice which is unnecessary. Any thoughts? Here's the block of code:


Dim myFldNm As String Dim myFld As Field
For Each myFld In ActiveDocument.Sections.Last.Range.Fields 'Loop thru Fields in the Section
If myFld.Type = 13 Then ' wdFieldTOC Then
myFld.Select
Selection.Font.Bold = False
Selection.Font.Underline = wdUnderlineNone
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 10
Selection.ParagraphFormat.SpaceAfter = 0
Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6.5), Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces 'Eliminates all the dotted lines
With Selection.Find
.Forward = True
.Text = vbTab 'Find the Tab
.Wrap = wdFindContinue
.MatchWildcards = True
Do While .Execute = True
Selection.Font.Underline = wdUnderlineDotted
Loop
Exit For
End With
End If
Next

Mavila
06-22-2022, 06:16 PM
I've discovered that it goes back to the beginning of the document each loop for some reason. It doesn't restrict the Find to the selected TOC in the Last section. It even formats other instances of vbtab that are not in the TOC.

Mavila
06-22-2022, 08:24 PM
Nevermind. Simple solution if you know what a tab leader is. This simple code works perfectly:



Dim aTOC As TableOfContents
For Each aTOC In ActiveDocument.TablesOfContents
aTOC.TabLeader = wdTabLeaderDots
Next

macropod
06-22-2022, 08:41 PM
I'm working with a large document with multiple TOCs (each chapter has a TOC). I was looping through all of the TOCs after the document was constructed to update the field code and apply formatting because of some strange behavior.
You haven't told us what the 'strange behavior' is, so it's impossible to advise on what the best approach might be. Quite possible, it requires nothing more complex than modifying a few Styles. There are other possibilities too, but without actually seeing the problem document, it would be difficult to give the right advice. Dealing with the problem at its source is far better than having to reformat the TOCs every time an update is needs.

Can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.

Mavila
06-23-2022, 09:51 AM
Thanks, I thought I posted a solution to this when I marked it solved.

Anyway, the strange behavior is a bit complicated to describe. I have several chapters, each with their own document. The longer chapters have a bookmark TOC. The user chooses which chapters to include in the Plan. The macro goes through the user's choices and then inserts them into the Plan with Selection.InsertFile.

Most of the time that works fine. However, randomly it seems, after the chapter is inserted, the TOC loses its formatting. The tab setting is eliminated (reverts to the default 0.5) and the dotted tableader disappears.

So, I was trying to reformat the TOC after the chapter is inserted and I couldn't find a way to do the tableader until I found

Dim aTOC As TableOfContents
For Each aTOC In ActiveDocument.TablesOfContents
aTOC.TabLeader = wdTabLeaderDots
Next