Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Add table of contents from mail merge

    Including a table of contents with a mail merge from access comes with a lot of errors. So I've created all my documents, they all have the correct headings and numbering.



    I would like a macro to go through a folder and insert a table of contents on page 2. Page 2 can be inserted as a new page if needs be. This is what I have right now:

    Code:
    Sub DoVBRoutineNow()
    Dim file
    Dim path As String
    
    path = "Y:\Name\Health & Safety\Word Docs\"
    file = Dir(path & "*.doc")
    Do While file <> ""
    Documents.Open Filename:=path & file
    Call secondSubRoutine
    ActiveDocument.Save
    ActiveDocument.Close
    file = Dir()
    Loop
    End Sub
    
    Sub secondSubRoutine()
       Application.Templates( _
            "C:\Users\ah\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Built-In Building Blocks.dotx" _
            ).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range _
            , RichText:=True
        Selection.TypeParagraph
    End Sub
    Nothing happens when clicked. I'm probably doing something wrong. The second sub routine was created when I clicked record macro in word and then added the table of contents. I then copied this code into the excel workbook where the code is living.

    If someone could help id really appreciate it.

  2. #2
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Code:
    Sub DoVBRoutineNow()
    Dim file
    Dim path As String
    
    path = "Y:\UU DWI Undertaking SR's\Health & Safety\Word Docs\"
    file = Dir(path & "*.docx")
    Do While file <> ""
    Documents.Open Filename:=path & file
    Call secondSubRoutine
    ActiveDocument.Save
    ActiveDocument.Close
    file = Dir()
    Loop
    End Sub
    
    Sub secondSubRoutine()
    Dim rangeWord As Integer
      Set rangeWord = ActiveDocument.Range(Start:=0, End:=0)
        ActiveDocument.TablesOfContents.Add rangeWord, _
                                        UseFields:=True, _
                                        UseHeadingStyles:=True, _
                                        LowerHeadingLevel:=3, _
                                        UpperHeadingLevel:=1
    End Sub
    Trying this now. Getting the error object required on this line :
    Code:
      Set rangeWord = ActiveDocument.Range(Start:=0, End:=0)
    I've put the definition of integer in myself. apparently incorrectly.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  4. #4
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Thanks, Can you see the issue with my reference?

  5. #5
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Code:
    Sub secondSubRoutine()
    Dim rangeWord As Integer
       rangeWord = ActiveDocument.Range(Start:=0, End:=0)
        ActiveDocument.TablesOfContents.Add rangeWord, _
                                        UseFields:=True, _
                                        UseHeadingStyles:=True, _
                                        LowerHeadingLevel:=3, _
                                        UpperHeadingLevel:=1
    End Sub
    Type mismatch now on "rangeWord" on this line:

    Code:
      ActiveDocument.TablesOfContents.Add rangeWord,
    Last edited by Homegrownandy; 08-04-2017 at 07:02 AM. Reason: wrong line

  6. #6
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

  7. #7
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Disclaimer: I have never worked with MS Word automation, but why is rangeword declared as an integer?
    I would expect it to be of type Range.

    Look at the provided example (from the link orange posted)
    Code:
     Dim myRange as Range    '<<-- I added this line. 
    
    Set myRange = ActiveDocument.Range(0, 0) 
    ActiveDocument.TablesOfContents.Add _ 
     Range:=myRange, _     '  <<<----need something like this
     UseFields:=False, _ 
     UseHeadingStyles:=True, _ 
     LowerHeadingLevel:=3, _ 
     UpperHeadingLevel:=1, _ 
     AddedStyles:="myStyle, yourStyle"
    Your code does not have " Range:= rangeWord, " in the parameters.

    You might open MS Word, press <alt>F11, open help from the IDE, search for "Range"......

  8. #8
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Having some IT issues here preventing me from working on office. Seems to be okay right now.

    this line:
    Code:
    Documents.Open Filename:=file
    Is saying object required. The code originally used was full of errors which I've since realised. All I'm trying to do now is open the documents. Ill work on subroutine 2 once I have this working.

  9. #9
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Thanks for the advice. I have this working now.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Andy,

    Can you tell us what you did and/or what was the issue? It may help others who browse or search the forum.

  11. #11
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    I stopped trying to get to the end and worked through the code. Its still not exactly what I want but it does work.

    This puts the TOC on page one. when I want to set the range as page two. (ill work on that when I get the chance)

    Code:
    Sub openword()
    
     Dim FSO As Object
     Dim fPath As String
     Dim myFolder, myFile
     Dim wdApp As Object 'Late Binding
     Set FSO = CreateObject("Scripting.FileSystemObject")
     With Application.FileDialog(msoFileDialogFolderPicker)
     .AllowMultiSelect = False
     .Show
     If .SelectedItems.Count > 0 Then fPath = .SelectedItems(1) & "\"
     End With
     On Error Resume Next
     Set myFolder = FSO.GetFolder(fPath).Files
     If Err.Number <> 0 Then 'Cancel button clicked.
     Exit Sub
     End If
     For Each myFile In myFolder
     If LCase(myFile) Like "*.doc" _
     Or LCase(myFile) Like "*.docx" Or LCase(myFile) Like "*.docm" Then
     On Error Resume Next
     Set wdApp = GetObject(, "Word.Application")
     If Err.Number <> 0 Then 'Word not yet running
     Set wdApp = CreateObject("Word.Application")
     End If
     
     wdApp.Documents.Open CStr(myFile)
     Call secondSubRoutine
     wdApp.Visible = True
     Set wdApp = Nothing
     End If 'LCase
     Next myFile
    
    End Sub
    Sub secondSubRoutine()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Word.Range
      Set oRng = ActiveDocument.Range
      oRng.Collapse
      
      With ActiveDocument
        .TablesOfContents.Add Range:=oRng, RightAlignPageNumbers:= _
          True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _
          :="SpecTOC,1", UseHyperlinks:=True, HidePageNumbersInWeb:=True, _
          UseOutlineLevels:=False
        .TablesOfContents(1).TabLeader = wdTabLeaderDots
        .TablesOfContents.Format = wdIndexIndent
      End With
    
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Mail Merge
    By sakhtar in forum Access
    Replies: 8
    Last Post: 09-20-2020, 09:10 AM
  2. Table Mail Merge - Is this possible?
    By janthony in forum Import/Export Data
    Replies: 3
    Last Post: 02-15-2015, 06:41 PM
  3. Mail Merge
    By zaffar_mughal in forum Access
    Replies: 1
    Last Post: 12-01-2013, 08:47 AM
  4. Help with Mail Merge please ?
    By bellevue in forum Forms
    Replies: 21
    Last Post: 02-16-2013, 07:05 AM
  5. Mail Merge
    By Nixx1401 in forum Access
    Replies: 1
    Last Post: 02-15-2010, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums