Consulting

Results 1 to 5 of 5

Thread: Tables of Contents and Using .Find with a Do While .Execute = True

  1. #1
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location

    Tables of Contents and Using .Find with a Do While .Execute = True

    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

  2. #2
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    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.

  3. #3
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    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

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Mavila View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •