Trying to Use Excel VBA to create bookmarks (in order) while inserting files

New Here ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Hi,

I have an Excel table of data that has columns containing full .pdf file names/locations.  I'm trying to insert each file into a master pdf file, and do 2 things:

  • Add a bookmark to the location of the new file in the master file, in order. I have gotten the bookmarks to show up, but they are currently showing up out of order, and/or reversed.
  • Ideally I would like the Bookmarks Navigation Pane to show as the default view, though this is 2nd priority.

 

With adding the bookmarks, I'm getting a "Run-Time error 438:

Object doesn't support this property or method" when I get to "n = UBound (BookmarkRoot.Children) +1" or any other reference to "BookmarkRoot.Chldren"

 

I'm new to manipulating PDFs with VBA, so any help is much appreciated!

 

 

 

Here is the more relevant code to this section:

 

                    Dim numPages As Integer
                    Dim n As Long
                    ' p As Long
                    Dim JSO As Object, BookmarkRoot As Object
                    Dim BookmarkFileName As String
                    Dim BookmarkFilePath As String

            Dim AcroApp As Acrobat.CAcroApp
            Dim Part1Document As Acrobat.CAcroPDDoc
       
            Set AcroApp = CreateObject("AcroExch.App")
            Set Part1Document = CreateObject("AcroExch.PDDoc")
           
            StrName = PatnerName
            'StrName = arrayFilePaths(1, 1)
           
            For j = 1 To Len(StrNoChr)
            StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
            Next
            StrName = Trim(StrName)
           
            StrFolder = "F:\Documents\"
            coverFileName = StrFolder & StrName & ".pdf"
           
            Debug.Print coverFileName & " is the Cover File"
           
          '=======================================================================
           
            ' Part1Document.Open (arrayFilePaths(1, 1))
            Part1Document.Open (coverFileName)
            '===========================================================================
       
            If Part1Document.Save(PDSaveFull, cpcFileSaveName) = False Then
                MsgBox "Cannot save the modified document"
            End If
              
            Part1Document.Close
       
       
            'AcroApp.Exit
            'Set AcroApp = Nothing
            'Set Part1Document = Nothing
       
            Debug.Print "Done renaming the Primary file"

    '===================================================
    Dim arrayIndex As Long
   
    For arrayIndex = 2 To UBound(arrayFilePaths, 2)
      '==========================================================================
            If arrayFilePaths(1, arrayIndex) <> "null" And VBA.Trim(arrayFilePaths(1, arrayIndex)) <> vbNullString Then
   
                    'Dim AcroApp As Acrobat.CAcroApp
               
                    'Dim Part1Document As Acrobat.CAcroPDDoc
                    Dim Part2Document As Acrobat.CAcroPDDoc
               
'                    Dim numPages As Integer
'                    Dim n As Long
'                    ' p As Long
'                    Dim JSO As Object, BookmarkRoot As Object
'
'                    Dim BookmarkFileName As String
'                    Dim BookmarkFilePath As String
                   
               
                    Set AcroApp = CreateObject("AcroExch.App")
               
                    Set Part1Document = CreateObject("AcroExch.PDDoc")
                    Set Part2Document = CreateObject("AcroExch.PDDoc")
               
                    Part1Document.Open (cpcFileSaveName)
                    Part2Document.Open (arrayFilePaths(1, arrayIndex))
                   
                    BookmarkFilePath = Left(arrayFilePaths(1, arrayIndex), Len(arrayFilePaths(1, arrayIndex)) - 4)
                    BookmarkFileName = Right(BookmarkFilePath, Len(BookmarkFilePath) - InStrRev(BookmarkFilePath, "\"))
                    Debug.Print "Bookmark name is: " & BookmarkFileName
               
                    ' Insert the pages of Part2 after the end of Part1
                    numPages = Part1Document.GetNumPages()
               
                    If Part1Document.InsertPages(numPages - 1, Part2Document, 0, Part2Document.GetNumPages(), True) = False Then
                        MsgBox "Cannot insert pages"
                       
                    'CREATE BOOKMARKS HERE
                    ElseIf Part1Document.InsertPages(numPages - 1, Part2Document, 0, Part2Document.GetNumPages(), True) = True Then
                                   
                                        Set JSO = Part1Document.GetJSObject
                                        Set BookmarkRoot = JSO.BookmarkRoot
                                       
                                    ' Children = BookmarkRoot.Children
                                    '   n = UBound(Children) - LBound(Children) + 1
  
                                    ' n = UBound(BookmarkRoot.Children) - LBound(BookmarkRoot.Children) + 1
                                   
                     
                                   
                                 n = UBound(BookmarkRoot.Children) + 1   'This was in the sample code I worked from
                                 Debug.Print n  ' BookmarkRoot.Children
                                                                        
                                    BookmarkRoot.createChild BookmarkFileName & " Bookmark " & n & " Page "_
& numPages + 1, "this.pageNum=" & numPages, n
                                    '   BookmarkRoot.createChild BookmarkFileName, n
                    End If
               
                    If Part1Document.Save(PDSaveFull, cpcFileSaveName) = False Then
                        MsgBox "Cannot save the modified document"
                    End If
               
                    Part1Document.Close
                    Part2Document.Close
               
                    AcroApp.Exit
                    Set AcroApp = Nothing
                    Set Part1Document = Nothing
                    Set Part2Document = Nothing
                    Debug.Print "Done merging the two files"
            End If
       
    Next arrayIndex
   
    Next r
   
    app.Exit
    Set app = Nothing

 

 

 

TOPICS
Document Generation API, General, How to

Views

11

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation