Why does adding MsgBox change my results?

Access9001

Registered User.
Local time
Today, 03:37
Joined
Feb 18, 2010
Messages
268
I am trying to add bookmarks dynamically in VBA to a pdf file. Here is the relevant code:


For Each element In OfficeArray
AVYourDoc.FindText element, True, True, True
AcroApp.MenuItemExecute ("NewBookmark")
PDBookmarker.GetByTitle PDYourDoc, "Untitled"
PDBookmarker.SetTitle (element)
Msgbox element
Next element

The problem is that it works right with the msgbox (but I have to click through them all). But without the msgbox, it'll add the first bookmark and then just keep adding the second one over and over again (although the bookmarks go to the right places, they're named incorrectly). What might be the reason?
 
Why does msgbox seem to change my subroutine?

I am trying to add bookmarks dynamically in VBA to a pdf file. Here is the relevant code:


For Each element In OfficeArray
AVYourDoc.FindText element, True, True, True
AcroApp.MenuItemExecute ("NewBookmark")
PDBookmarker.GetByTitle PDYourDoc, "Untitled"
PDBookmarker.SetTitle (element)
Msgbox element
Next element

The problem is that it works right with the msgbox (but I have to click through them all). But without the msgbox, it'll add the first bookmark and then just keep adding the second one over and over again (although the bookmarks go to the right places, they're named incorrectly). What might be the reason?
 
Re: Why does msgbox seem to change my subroutine?

Don't know for sure but have you set a breakpoint at the beginning of the code (without the msgbox) and stepped through to see what is happening?
 
Re: Why does msgbox seem to change my subroutine?

Yes. It just doesn't make any sense to me whatsoever why it's repeating the second bookmark name.
 
Re: Why does msgbox seem to change my subroutine?

So it doesn't do it if you set a breakpoint and then step through it?
 
Re: Why does msgbox seem to change my subroutine?

It still fails when I breakpoint. It works correctly when I have the msgbox.
 
Re: Why does msgbox seem to change my subroutine?

I'm wondering if it isn't a TIMING issue. When you do the breakpoint and step through it, what happens if you wait 5 seconds before pressing F8 after running this line:
PDBookmarker.SetTitle (element)
 
Re: Why does msgbox seem to change my subroutine?

Same error. I had also tried synthesizing something similar with a msgbox that closed after X seconds by itself. It only seems to work with a msgbox that I literally have to go click.
 
Re: Why does msgbox seem to change my subroutine?

This is driving me nuts. This makes no sense -- why on earth is it only working with a msgbox?
 
Re: Why does msgbox seem to change my subroutine?

Might be an idea to show the full code.
 
Re: Why does msgbox seem to change my subroutine?

Sub bookmarker()

Dim PDBookmarker As CAcroPDBookmark
Dim AcroApp As CAcroApp
Dim AVYourDoc As CAcroAVDoc
Dim PDYourDoc As CAcroPDDoc
Dim OfficeArray As Variant

OfficeArray = Array("Atlanta", "Boston", "Casper", "Denver", "Frankfurt")


Set AcroApp = CreateObject("AcroExch.App")
Set PDYourDoc = CreateObject("AcroExch.PDDoc")
Set AVYourDoc = CreateObject("AcroExch.AVDoc")
Set PDBookmarker = CreateObject("AcroExch.PDBookmark")


If Not PDYourDoc.Open("C:\MainData.pdf") Then
MsgBox "Cannot open file"
End
End If

Set AVYourDoc = PDYourDoc.OpenAVDoc("")


'set bookmark title
For Each element In OfficeArray
AVYourDoc.FindText element, True, True, True
AcroApp.MenuItemExecute ("NewBookmark")
PDBookmarker.GetByTitle PDYourDoc, "Untitled"
PDBookmarker.SetTitle (element)
'msgbox element <-uncommenting this line lets it work properly, but you have to click OK a bunch
Next element

AVYourDoc.Close (False)
Set AVYourDoc = Nothing
Set PDYourDoc = Nothing
Set AcroApp = Nothing

End Sub
 
Last edited:
Re: Why does msgbox seem to change my subroutine?

Did you write this code yourself? Have you got the class for the CAcroApp object?
 
Re: Why does msgbox seem to change my subroutine?

Yes, I should -- I have the full version of Acrobat. This code was pieced together from various Google sources and adjusted to my needs.
 
Re: Why does msgbox seem to change my subroutine?

Declare OfficeArray as an array of type String.

Dim OfficeArray() as String
 
Re: Why does msgbox seem to change my subroutine?

Gives me a type mismatch when I try to actually declare my array.
 
Re: Why does msgbox seem to change my subroutine?

Oops.. I was thinking of .net :)

Dim OfficeArray() As Variant
 
Re: Why does msgbox seem to change my subroutine?

Same error with the bookmarks, unfortunately
 
Re: Why does msgbox seem to change my subroutine?

Where is it showing the error?
 
Re: Why does msgbox seem to change my subroutine?

When I run the code, the pdf opens and it shows the bookmarks being added in real time. It should be adding the Atlanta bookmark to the Atlanta page (where the text "Atlanta" is found), Boston to the Boston page, etc. It works fine with the message box, but without it, it'll post Atlanta correctly, then repeat Boston as the name of all the bookmarks from that point forward (even though they all point to the correct destinations)
 
Re: Why does msgbox seem to change my subroutine?

I see what you mean. Did you try the GetTitle method of PDBookmarker instead of GetByTitle?

Send me a sample pdf with those texts in them.
 

Users who are viewing this thread

Back
Top Bottom