Trying to Use Excel VBA to create bookmarks 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
Edit and convert PDFs, General troubleshooting

Views

218

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
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

LATEST

So you have to be careful when mixing JavaScript objects and VBA objects. Scaler values and simple generic arrays seems to work fine, but there are large incompatibilities when dealing with the DOM objects, i.e "bookmarkRoot.children" (BTW, JavaScript is case sensitive and "children" is lower case. I don't know if this is the issue, but another compatibility issue). In fact the best strategy is not to do it at all.  Create the functionality you want to implement in Acrobat JavaScript as a folder level function, then call this function from the JSO in VBA. You then verify the JS in it's native environment and avoid all of the efficientcy and compatibility issues associated with crossing the ActiveX boundary.  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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