Consulting

Results 1 to 6 of 6

Thread: Modifying Table of Contents with VBA

  1. #1
    VBAX Newbie
    Joined
    Nov 2019
    Posts
    3
    Location

    Modifying Table of Contents with VBA

    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
    --------------------------------------------------

    TOC_Heading1_Mod.JPG

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,883
    Location
    In the Word document, change the TOC1 style paragraph format to "Page Break Before"


    Capture.JPG
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Nov 2019
    Posts
    3
    Location
    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,

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

  5. #5
    VBAX Newbie
    Joined
    Nov 2019
    Posts
    3
    Location
    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
    Ed With

    Thanks for the help.

  6. #6
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,883
    Location
    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.
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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