Searching a .pdf using Excel 2010 VBA

AverageAmy

New Member
Joined
Nov 26, 2011
Messages
25
I am trying to open and search a .pdf using VBA in Excel. Ultimately, I want to search the pdf and put the first five characters following the found text from the .pdf in a cell in Excel. However, I am having issues with doing anything with the application. More details on the error I encounter at the end.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I am using Excel 2010, Acrobat Standard 9, and am on Windows 7. The References I have active are Visual Basic for Applications; Microsoft Excel 14.0 Object Library; OLE Automation; Adobe Acrobat 9.0 Type Library; and Microsoft Office 14.0 Object Library. The code shown below does not work with this setup, BUT it does work with Excel 2000, Acrobat Standard 8, and Windows XP.<o:p></o:p>
<o:p></o:p>
Code:
Sub AcrobatFindText2() <o:p></o:p>
<o:p></o:p>
'variables<o:p></o:p>
Dim Resp 'For message box responses<o:p></o:p>
Dim gPDFPath As String<o:p></o:p>
Dim sText As String 'String to search for<o:p></o:p>
Dim sStr As String 'Message string<o:p></o:p>
Dim foundText As Integer 'Holds return value from "FindText" method<o:p></o:p>
<o:p></o:p>
'hard coding for a PDF to open, it can be changed when needed.<o:p></o:p>
gPDFPath = "C:\Users\Me\Documents\test.pdf"<o:p></o:p>
<o:p></o:p>
'Initialize Acrobat by creating App object<o:p></o:p>
Set gApp = CreateObject("AcroExch.App", "")<o:p></o:p>
gApp.Hide<o:p></o:p>
<o:p></o:p>
'Set AVDoc object<o:p></o:p>
Set gAvDoc = CreateObject("AcroExch.AVDoc")<o:p></o:p>
<o:p></o:p>
' open the PDF<o:p></o:p>
If gAvDoc.Open(gPDFPath, "") Then<o:p></o:p>
sText = "Designation"<o:p></o:p>
'FindText params: StringToSearchFor, caseSensitive (1 or 0), WholeWords (1 or 0), 'ResetSearchToBeginOfDocument (1 or 0)<o:p></o:p>
foundText = gAvDoc.FindText(sText, 1, 0, 1) 'Returns -1 if found, 0 otherwise<o:p></o:p>
<o:p></o:p>
Else ' if failed, show error message<o:p></o:p>
Resp = MsgBox("Cannot open" & gPDFPath, vbOKOnly)<o:p></o:p>
<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
If foundText = -1 Then<o:p></o:p>
<o:p></o:p>
'compose a message<o:p></o:p>
sStr = "Found " & sText<o:p></o:p>
Resp = MsgBox(sStr, vbOKOnly)<o:p></o:p>
Else ' if failed, 'show error message<o:p></o:p>
Resp = MsgBox("Cannot find" & sText, vbOKOnly)<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
gApp.Show<o:p></o:p>
gAvDoc.BringToFront<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
When I try to run this on my main system (Excel 2010, Acrobat 9, Win7), I get a run-time error on
Code:
gApp.Hide
It will say: <o:p></o:p>
Run-time error ‘-2147319799 (8002801d)’:<o:p></o:p>
Automation error<o:p></o:p>
Library not registered.<o:p></o:p>
I went into Tools and selected all the References that I thought might remotely be related to Acrobat, and the same thing happened.<o:p></o:p>

If I comment that line out then run, Excel “stops working” and crashes at:
Code:
If gAvDoc.Open(gPDFPath, "") Then<o:p></o:p>
<o:p></o:p>
If I remove the If statement and go directly to:
Code:
foundText = gAvDoc.FindText(sText, 1, 0, 1)
I get the same run-time error shown above. <o:p></o:p>
<o:p></o:p>
Given this and the fact that the code works on my older system (Excel 2000, Acrobat 8, WinXP), I think this has something to do with the Adobe operations allowed in Excel 2010, however I have searched online and can’t find a solution.<o:p></o:p>
<o:p></o:p>
Can anyone out there help?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Update: I copied my .xlsm file containing this code to another system using Excel 2010 and it works there.
 
Upvote 0
Another update: I have tried repairing my Excel 2010 installation and that didn't work. I can't figure out why the one version of Excel 2010 won't run this macro.

Any thoughts of where to look next?
 
Upvote 0
Got It!! :biggrin:
The problem was with Adobe Acrobat Standard 9. There are known issues between Acrobat 9.3 (and later) and Office 2010 where Acrobat does not show up in the ribbon.
See: (http://kb2.adobe.com/cps/843/cpsid_84399.html).
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p>My main computer where I was having problems running the macro had Acrobat Standard 9.5, whereas the other computer had Acrobat Standard 9.0. I deactivated and uninstalled 9.5, and then installed 9.0 and all works fine.</o:p>
 
Upvote 0
Hey, thanks for the code above. Do you know how to use this to extract the next X number of characters after finding the string you are searching for?
 
Upvote 0
Hey, thanks for the code above. Do you know how to use this to extract the next X number of characters after finding the string you are searching for?

I don't really, but there is a document that tries to explain the commands for working with pdf documents. See Adobe Acrobat and VBA – An Introduction | Karl Heinz Kremer's Ramblings and linked documents: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/iac_developer_guide.pdf and http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/iac_api_reference.pdf.

It's been a while since I worked with this, but I seem to remember searching online, finding snippets of code, and trial and error to finally get something that worked. Page 30 on the developer guide pdf and Page 44 on the API reference pdf seem to point you in the right direction, though. Please post back if you find something that works - I would love to hear it.
 
Upvote 0

Forum statistics

Threads
1,215,206
Messages
6,123,638
Members
449,109
Latest member
Sebas8956

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
Back
Top