Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Feb 2004
    Location
    Chicago, Illinois, USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loop Through TOC Table (VBA/Word 2002)

    Due to a client using a third-party package, when we regenerate their TOC in Word 2002 it brings in the entire paragragh due to the formatting of their file. I wrote a macro that once the TOC is generated it will delete the extra text. But I am having difficulties doing it using a loop, so right now I have the secretary pressing a speed key which goes line by line through the resulting table. I have tried getting it to work but have not had success. Below is what works line by line.

    Selection.Paragraphs.First.Range.Select

    Selection.Font.Reset
    Selection.Find.ClearFormatting

    With Selection.Find
    .Text = "."
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Find.Execute
    Selection.Extend
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "^t"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1
    End Sub


    Hoping someone can give me some assistance,
    Mary Lou Palace

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Loop Through TOC Table (VBA/Word 2002)

    <P ID="edit" class=small>(Edited by jscher2000 on 31-Mar-05 10:57. Added a note to the code.)</P>So this macro finds the next period, select to the next tab, backs off the tab so that the tab is not selected, then deletes the text and positions at the beginning of the next line? It is not too hard to wrap this into a loop, but it might be better to make it a wildcard search and replace. Either way, the only trick is to make sure that only the TOC is affected, or much of the rest of the document might get wrecked!

    The wildcard option might look like this:

    <code>Sub TrimParaInTOC()
    ' In the current section, search for the pattern ".<anything><tab>"
    ' and replace it with "<tab>"
    Dim rng As Word.Range
    ' Set the range for find and replace to the current section... this can be set to whatever you like
    Set rng = ActiveDocument.Sections(Selection.Information(wdAc tiveEndSectionNumber)).Range
    With rng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ".*^t"
    .Replacement.Text = "^t"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    ' Replace All
    .Execute Replace:=wdReplaceAll
    ' Reset sticky settings
    .MatchWildcards = False
    End With
    If Not (rng Is Nothing) Then Set rng = Nothing
    End Sub</code>


    This runs only for the section where the user has the insertion point, so if the TOC is in its own section, which is fairly common, that should be enough restriction. If there are other tables in there (e.g., Table of Authorities), the range would have to be set differently.

  3. #3
    New Lounger
    Join Date
    Feb 2004
    Location
    Chicago, Illinois, USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loop Through TOC Table (VBA/Word 2002)

    Code works to a point so I am trying to adjust it to met my needs. Any suggestions would be appreciated. The document uses legal numbering 2.1, 2.2 etc. on level 2 so in the table the .1, .2, etc. is being stripped out (see my attachment for a sample). What I am doing in my code is looking for a period (the period that appears at the end of the text that would be the table entry), turning on extend, searching for the tab (which would be the leader tab) and deleting what was selected. Then I strip the formatting off of the line because it has direct formatting and I cannot just apply a style code.

    The other issue is there is a page break at the end of the table and not a section break because there is a manual TOA in the document. I can't add any formatting to the original because this file is being round tripped to the client and they have told us we cannot change any of their formatting.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Loop Through TOC Table (VBA/Word 2002)

    Hmmm... so you want periods with a space after them, not a tab after them. Except when there is only one sentence. That latter one probably has to be handled as an exceptional case, so I'll skip it for now. The main procedure would be:

    <code>Sub TrimParaInTOC2()
    ' In the current page, search for the pattern ".<space><anything><tab>"
    ' and replace it with "<tab>"
    Dim rng As Word.Range
    ' Set the range for find and replace to the current page... this can be set to whatever you like
    Set rng = ActiveDocument.Bookmarks("PAGE").Range
    rng.Select
    Stop
    With rng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ". *^t"
    .Replacement.Text = "^t"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    ' Replace All
    .Execute Replace:=wdReplaceAll
    ' Reset sticky settings
    .MatchWildcards = False
    End With
    If Not (rng Is Nothing) Then Set rng = Nothing
    End Sub</code>

    Does it get you 98% of the way there in the real document?

  5. #5
    New Lounger
    Join Date
    Feb 2004
    Location
    Chicago, Illinois, USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loop Through TOC Table (VBA/Word 2002)

    I added the line "Selection.Font.Reset" before the With to remove the direct formatting but otherwise it is perfect! Thanks so very much.

Posting Permissions

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