• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Add bookmarks to pdf file using Excel VBA

Guest
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

37.1K

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

correct answers 1 Correct answer

Engaged , Sep 16, 2009 Sep 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

Votes

Translate

Translate
Engaged ,
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

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

Votes

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
Guest
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

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.

Votes

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
Enthusiast ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

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.

Votes

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
Guest
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

Hi Patrick -

Thanks for the information.  I'm not exactly sure how to implement that.  Would you have an example?

Votes

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
Engaged ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

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

Votes

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
Guest
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

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?

Votes

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
Engaged ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

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

Votes

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
Guest
Sep 17, 2009 Sep 17, 2009

Copy link to clipboard

Copied

Thanks for the information.  Using that example I determined why the sendkeys wasn't working.  It appears that I must show the app as well as use appactivate before the sendkeys.  All is working now.  Thank you very much for all of your assistance.

Votes

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
New Here ,
Apr 07, 2023 Apr 07, 2023

Copy link to clipboard

Copied

LATEST

Hi could you please share the code? I am working on similar automation to add bookmarks in pdf.

Votes

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
New Here ,
Aug 12, 2019 Aug 12, 2019

Copy link to clipboard

Copied

Hello ReinhardF,

A few years later, but still people are searching for ways to make bookmarks in e tree like fasion using VBA.

Unfortunally the file you mentioned, http://www.ReFob.de/downloads/Acrobat/AcroJsBookmarksVBA.zip, does no longer exist.

Is there a way that i can retrieve this file?

Regards Jos

Votes

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