Skip to main content
September 15, 2009
Answered

Add bookmarks to pdf file using Excel VBA

  • September 15, 2009
  • 2 replies
  • 40081 views

Good morning -

I have, through internet searches, created an Excel macro that prints numerous workbooks to pdf, combines them into one file and prints that file.  I have been searching for the last piece, which is adding bookmarks, but have not had any luck getting any of it to work.  The following is the code that I am using to add the bookmarks -

Dim avdoc, pddoc, app As Object
Dim stFile As String
Dim btitle As Boolean
Dim PDBookmark As CAcroPDBookmark

stFile = "D:\Test\Test.pdf"
Set app = CreateObject("AcroExch.app")

Set avdoc = CreateObject("AcroExch.AVDoc")
Set pddoc = CreateObject("AcroExch.PDDoc")
pddoc.Open stFile

Set avdoc = pddoc.OpenAVDoc(stFile)
Set PDBookmark = CreateObject("AcroExch.PDBookmark", "")

app.MenuItemExecute ("NewBookmark")

btitle = PDBookmark.SetTitle("Test Bookmark")

This code does insert a bookmark on the first page of the document.  The bookmark title displays as Untitled instead of Test Bookmark as the code states.  Can someone answer the following questions -

1.  Why is the code not changing the bookmark from Untitled to Test Bookmark, and

2.  How would I nodify the above code to go to a certain page then enter the bookmark.

All help is greatly appreciated.  Thanks in advance for your time.

This topic has been closed for replies.
Correct answer ReinhardF

Hi,

these discussion reminds me to an excellent VBA example from Jens Kammerath,

which also answer your next question = how to set up bookmarks as tree.

Also save some work and testing :-)).

You can download from:

http://www.ReFob.de/downloads/Acrobat/AcroJsBookmarksVBA.zip

HTH, Reinhard

2 replies

ReinhardF
Participating Frequently
September 16, 2009

Oooh,

forgot it.

with:

app.MenuItemExecute ("NewBookmark")

you set Acrobat into edit mode (you may test it manual).

So after that you have to use sendkey with Enter or ESC to finish edit mode.

Then you can get and set the title.

If you are at this state you may think about to use only sendkey for all 3 commands.

SendKeys "^b" & "Text" & "{Enter}", True

best regards, Reinhard

September 16, 2009

Thanks for the quick response. I see what you mean about the edit mode. I replaced the app.MenuItemExecute ("NewBookmark") with SendKeys "^b" & "Text" & "{Enter}", True but it does not do anything now. It appears to run the line of code just does not seem to activate the menu.  Any ideas?

ReinhardF
ReinhardFCorrect answer
Participating Frequently
September 16, 2009

Hi,

these discussion reminds me to an excellent VBA example from Jens Kammerath,

which also answer your next question = how to set up bookmarks as tree.

Also save some work and testing :-)).

You can download from:

http://www.ReFob.de/downloads/Acrobat/AcroJsBookmarksVBA.zip

HTH, Reinhard

ReinhardF
Participating Frequently
September 15, 2009

1. You first have to get it, then you can set it.

something like(untested):

app.MenuItemExecute ("NewBookmark")
PDBookmark.GetByTitle PDDoc, "Untitled"
btitle = PDBookmark.SetTitle("Test Bookmark")

2. Before you create the bookmark you have to go to to the page - like you would do it manual.

HTH, Reinhard

September 16, 2009

Good morning Reinhard -

Thanks you very much for the help.  I implemented as follows:

1.  app.MenuItemExecute ("NewBookmark")

btitle = PDBookmark.GetByTitle(pddoc, "Untitled")
btitle = PDBookmark.SetTitle("Total Accessories / Hardgoods")

I added the "btitle =" due to it erroring out.  However, when I step through the code and get to that line, I get the "Excel has encountered an error and needs to close" error.  Is there a reference that I'm missing that you know of?

2.  I was able to find the code to get to a specific page number and it is working great.

Thanks again for all of your assistance.

Patrick_Leckey
Participating Frequently
September 16, 2009

You have to make sure btitle is not null after getting the reference to the bookmark, and then call SetTitle on the bookmark objects, not on the PDBookmark class.