macro to bookmark a pdf file

Tintoo

New Member
Joined
Oct 9, 2015
Messages
27
Hi experts.. below is my issue

I have a pdf file and am trying to insert bookmarks in that pdf file based on the cell text and page numbers mentioned in excel. In excel i have mentioned the page number to be bookmarked and the bookmark title content. When i execute this i receive an end of statement error. Please help to resolve this.

*NOTE: Am not trying to convert the excel into pdf with bookmarks. I need to insert the bookmarks that i specify in excel into an existing pdf file.

Below is the code am using.

VBA Code:
Sub Add Bookmark()

Dim AcroApp As Acrobat.CAcroApp
Dim PDoc As Acrobat.CAcroPDDoc
Set AcroApp = CreateObject(“AcroExch.App”)
Set PDoc = CreateObject(“AcroExch.PDDoc”)

PDoc.Open (ThisWorkbook.Worksheets(“Add Bookmark”).Cells(2, 1))
Set jso = PDoc.GetJSObject

jso.BookmarkRoot.Remove

Set BMR = jso.BookmarkRoot
a = 0
For i = 2 To ThisWorkbook.Sheets(“Add Bookmark”).Range(“B” & Rows.Count).End(xlUp).Row
pg = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 3) – 1
bm = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 2)
af = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 4)
bf = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 5)
cf = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 6)
df = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 7)

If pg > “” And bm > “” And (af > “” Or bf > “” Or cf > “” Or df > “”) Then
If af = “Y” Then
BMR.createChild bm, “this.pageNum = ” & pg, a
BMA = BMR.Children
Set oBMA = BMA(a)
a = a + 1
b = 0
c = 0
d = 0
End If
If bf = “Y” Then
oBMA.createChild bm, “this.pageNum = ” & pg, b
BMB = oBMA.Children
Set oBMB = BMB(b)
b = b + 1
c = 0
d = 0
End If
If cf = “Y” Then
oBMB.createChild bm, “this.pageNum = ” & pg, c
BMC = oBMB.Children
Set oBMC = BMC(c)
c = c + 1
d = 0
End If
If df = “Y” Then
oBMC.createChild bm, “this.pageNum = ” & pg, d
d = d + 1
End If
Else
MsgBox “Row: ” & i & “, title or page number or bookmark level missing!”
Exit Sub
End If

Next i

n = PDoc.Save(PDSaveFull, ThisWorkbook.Worksheets(“Add Bookmark”).Cells(2,1))

PDoc.Close

AcroApp.Exit
Set AcroApp = Nothing
Set PDoc = Nothing
Set ADoc = Nothing

MsgBox “Done!”

End Sub
 
Last edited by a moderator:

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

colmsmyth

New Member
Joined
Jun 15, 2012
Messages
12
Hi experts.. below is my issue

I have a pdf file and am trying to insert bookmarks in that pdf file based on the cell text and page numbers mentioned in excel. In excel i have mentioned the page number to be bookmarked and the bookmark title content. When i execute this i receive an end of statement error. Please help to resolve this.

*NOTE: Am not trying to convert the excel into pdf with bookmarks. I need to insert the bookmarks that i specify in excel into an existing pdf file.

Below is the code am using.

VBA Code:
Sub Add Bookmark()

Dim AcroApp As Acrobat.CAcroApp
Dim PDoc As Acrobat.CAcroPDDoc
Set AcroApp = CreateObject(“AcroExch.App”)
Set PDoc = CreateObject(“AcroExch.PDDoc”)

PDoc.Open (ThisWorkbook.Worksheets(“Add Bookmark”).Cells(2, 1))
Set jso = PDoc.GetJSObject

jso.BookmarkRoot.Remove

Set BMR = jso.BookmarkRoot
a = 0
For i = 2 To ThisWorkbook.Sheets(“Add Bookmark”).Range(“B” & Rows.Count).End(xlUp).Row
pg = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 3) – 1
bm = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 2)
af = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 4)
bf = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 5)
cf = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 6)
df = ThisWorkbook.Sheets(“Add Bookmark”).Cells(i, 7)

If pg > “” And bm > “” And (af > “” Or bf > “” Or cf > “” Or df > “”) Then
If af = “Y” Then
BMR.createChild bm, “this.pageNum = ” & pg, a
BMA = BMR.Children
Set oBMA = BMA(a)
a = a + 1
b = 0
c = 0
d = 0
End If
If bf = “Y” Then
oBMA.createChild bm, “this.pageNum = ” & pg, b
BMB = oBMA.Children
Set oBMB = BMB(b)
b = b + 1
c = 0
d = 0
End If
If cf = “Y” Then
oBMB.createChild bm, “this.pageNum = ” & pg, c
BMC = oBMB.Children
Set oBMC = BMC(c)
c = c + 1
d = 0
End If
If df = “Y” Then
oBMC.createChild bm, “this.pageNum = ” & pg, d
d = d + 1
End If
Else
MsgBox “Row: ” & i & “, title or page number or bookmark level missing!”
Exit Sub
End If

Next i

n = PDoc.Save(PDSaveFull, ThisWorkbook.Worksheets(“Add Bookmark”).Cells(2,1))

PDoc.Close

AcroApp.Exit
Set AcroApp = Nothing
Set PDoc = Nothing
Set ADoc = Nothing

MsgBox “Done!”

End Sub

Did you ever get this working, I am trying to use the same code from the source you got this from and it just does not run.

I did get the Adobe Type Library installed but it seems the actual code is not formatted correctly.

So I am wondering if you got this working or if you came up with another solution ?
 

Watch MrExcel Video

Forum statistics

Threads
1,123,346
Messages
5,601,080
Members
414,426
Latest member
fraru

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
Top