VBA - Merge PDF - Bookmark with Filename

NewbieAU

New Member
Joined
Oct 25, 2021
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone. I am currently merging multiple PDFs together and was hoping for a solution to merge together with bookmarks.

This is what I have currently:

VBA Code:
    For n = 1 To UBound(PDFfiles)
        objCAcroPDDocDestination.Open Path & PDFfiles(n, 1)
                
        For i = 2 To 10
            If PDFfiles(n, i) <> "" Then
                objCAcroPDDocSource.Open Path & PDFfiles(n, i)
                    If Not objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
                    MsgBox "Error merging" & vbCrLf & PDFfiles(n, 1) & vbCrLf & "and" & vbCrLf & PDFfiles(n, i)
            End If
                    objCAcroPDDocSource.Close
            Else
            End If
        
        Next i

I found this (Create PDF bookmarks while combinding files with VBA in Excel) so tried to change the code to this:

Code:
    For n = 1 To UBound(PDFfiles)
    objCAcroPDDocDestination.Open Path & PDFfiles(n, 1)
                
    For i = 2 To 10
    
    Dim insertSuccess As Boolean
    Dim x As Long, p As Long
    Dim JSO As Object, BookMarkRoot As Object

        p = objCAcroPDDocDestination.GetNumPages

        If PDFfiles(n, i) <> "" Then
            objCAcroPDDocSource.Open Path & PDFfiles(n, i)

        If Not objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
            MsgBox "Error merging" & vbCrLf & PDFfiles(n, 1) & vbCrLf & "and" & vbCrLf & PDFfiles(n, i)
        End If

        insertSuccess = objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0)

        If insertSuccess Then
            x = x + 1
                Set JSO = objCAcroPDDocDestination.GetJSObject
                Set BookMarkRoot = JSO.BookMarkRoot
                BookMarkRoot.createchild "Bookmark " & x & " Page " & p + 1, "this.pageNum=" & p, n
            End If
                    objCAcroPDDocSource.Close
            Else
            End If
        
        Next i

My rubbish hack job of trying to slip in the code didn't kill the code. It's still merging - but not making the bookmarks. Where did I go wrong? Also how do I modify the Bookmark name so it's the name of the file I inserted?

Much thanks!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,215,884
Messages
6,127,564
Members
449,385
Latest member
KMGLarson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top