Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Word macro: skip to first paragraph after TOC

    I have a macro that steps through a Word document one paragraph at a time. I want the macro to skip the TOC fields (Table of Contents, Table of Illustrations and List of Tables) by moving to the first paragraph after the TOC field. The point is that I want to continue the macroís work right after the TOC, so looping through document fields and skipping the TOC field doesnít help.

    I canít find any method in the Range collection that would allow me to extend current paragraph range to the end of a field. I canít find any method for this in the Fields collection. I have searched the net and not found anything I can use.

    A workaround is to step through each paragraph, test whether that paragraph is in a TOC, and if it is then jump to the end of the loop and proceed to test the next paragraph. However, the macro is quite heavy and time consuming, so skipping the three TOCs that I mention above would be useful.

    Does anybody have an idea, or can someone point me to a website or forum thread where this is discussed?
    Stylus

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Are they all really TOCs?
    Code:
    Sub GotoRange()
      Dim aRng As Range
      Set aRng = ActiveDocument.TablesOfContents(ActiveDocument.TablesOfContents.Count).Range
      aRng.Collapse Direction:=wdCollapseEnd
      aRng.End = ActiveDocument.Range.End
      aRng.Select
    End Sub
    The tricky part is actually working out whether the last TOC is actually what you want. It is likely that you are looking for the last TableOfFigures rather than the last TableOfContents. If so, replace this word in the above code.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Stylus (2016-06-10)

  4. #3
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you Andrew - it never occurred to me to look at TablesOfContents (and Figures) instead of Fields...

    VBA is humbling, as ever.
    Stylus

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Another approach is to toggle Word's field code display 'on' with your macro. That way, multi-paragraph fields like TOC, TOF, TOA, Bibliography all collapse to a single paragraph and you can easily enough test whether a given paragraph in that state consists of a single field (and what kind). Simply toggle Word's field code display 'off' with your macro when done processing.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. The Following User Says Thank You to macropod For This Useful Post:

    Stylus (2016-06-10)

  7. #5
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you Paul - I had amended my reply to Andrew but was logged out by the time I hit enter, so a part was lost.

    The thing is that I want to format captions, so I don't want to skip those fields. My macro therefor goes like this:

    If .Range.Fields.Count <> 0 Then
    Select Case .Range.Fields(1).Type
    Case 8, 13, 47, 73, 97
    'skip the field

    I can't find a FieldType value for TOF. How can I determine if the field contained in the paragraph range is a TOF? Or is my approach mistaken?
    Stylus

  8. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Captions are easily-enough identified - they use SEQ fields (type 12) and, by default, will be in paragraphs with the Caption format and be named Equation, Figure, Table (or a custom name).
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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