Use MS Access 2003 VBA (not JavaScript) to find text in pdf using Acrobat X, then bookmark it
GalfromKalamazoo Jan 3, 2013 5:34 AMI originally posted this is the AcrobatUsers.com, but was advised to post it here instead.
Using Win XP Pro, Adobe X, and Access 2003:
This code runs thru a few files, then can't find text to be bookmarked (it's there, though). If run again, same thing with next few files.
There seem to be several issues going on here. I've observed when the code is running, and Acrobat is searching for the text to bookmark, sometimes it finds totally random text, rather than the precise text specified (e.g., 'Landfilled waste'). Other times, it finds the correct text and also selects several disconnected words in the vicinity! When the point is reached where Acrobat thinks it hasn't found the text, the code just hangs, waiting for Acrobat to do something, and then the message appears the the application is busy and I should either switch to that app, retry, or cancel, all of which fail to work.
I have also noticed that when it fails to find the requested text, when I manually search the file, it can't find it, either, even when I locate the text myself, highlight it and copy it into the search box! I also must state that this code worked perfectly a year ago, before Acrobat upgraded to version X. Is this a bug?
Private Sub cmdAddBookmark_Click()
'accesses the pdf reports using the Acrobat object
Dim BM As Object
Dim AcApp As Object
Dim ADoc As Object
Dim PDoc As Object
Dim bFileOpen As Boolean
Dim btitle As Boolean
Dim strfile As String
Dim fso As New Scripting.FileSystemObject
Dim fil As Scripting.File
Dim fldr As Scripting.Folder
Dim filn As String
Dim strB As String
'Create the Acrobat Object
Set AcApp = CreateObject("AcroExch.App")
'Create Doc objects
Set PDoc = CreateObject("AcroExch.PDDoc", "")
Set ADoc = CreateObject("AcroExch.AVDoc", "")
'the following variables are used to set the directory where the files are to be processed
If strYr = "" Then strYr = inputbox("Enter report data year", , Year(Now) - 2)
If strtype = "" Then strtype = inputbox("Enter Corp or Indiv type of report", "Corp or Indiv", "Corp")
If strReport = "" Then strReport = inputbox("Enter Energy, Env, or Combined for type of report", "Energy, Env, or Combined", "Energy")
If strReport = "Energy" Then
strB = "Appendix B - Fuel and Energy Use"
Else
strB = "Appendix B - Mill Environmental Data and Statistics"
End If
Set fso = CreateObject("Scripting.FileSystemObject")
'Set path to pdf files
Set fldr = fso.GetFolder(GetPath() & strYr & "\" & strReport & "\" & strtype & "\PDFRpts\")
'check for pdfs in folder
If Dir(fldr & "\*.pdf") <> "" Then
For Each fil In fldr.Files
filn = fil.Name
strfile = fldr & "\" & filn
'code to load doc
bFileOpen = ADoc.Open(strfile, "")
Set PDoc = ADoc.GetPDDoc
'Create BookMark Object
Set BM = CreateObject("AcroExch.PDBookmark")
'Show the Application to be able to insert bookmark
AcApp.Show
'if bookmark hasn't already been added...
If Not BM.GetByTitle(PDoc, "Appendix B") Then
'look for text to bookmark: case-sensitive, whole words only, starting from beginning
If ADoc.FindText(strB, True, True, True) Then
Me.lblBM.Caption = "Adding bookmark to " & filn & "."
Me.Repaint
'execute the menu item
AcApp.MenuItemExecute ("NewBookmark")
'locate new bookmark
btitle = BM.GetByTitle(PDoc, strB)
'set bookmark title
btitle = BM.SetTitle("Appendix B")
Else
Me.lblBM.Caption = "Could not find text for Appendix B in file " & filn
Me.Repaint
ADoc.Close 0
PDoc.Close
GoTo exit_insert_bookmark
End If
Else
Me.lblBM.Caption = filn & " already has a bookmark for " & strB
Me.Repaint
End If
PDoc.Save 1, strfile
ADoc.Close 0
PDoc.Close
Next fil
End If
Me.lblBM.Caption = ""
MsgBox "Done!"
exit_insert_bookmark:
AcApp.CloseAllDocs
AcApp.Exit
Set AcApp = Nothing
End Sub





