These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Find Text inside PDF's files using Vba with acropdf.dll

Angroberto38
Registered: Feb 26 2008
Posts: 4

I am from Brazil, I would like your help.

I want to know how to use VBA's commands to read and to find text inside of Pdf file, if posible send to me code in VBA

Thanks you

jdawson422
Registered: Feb 18 2008
Posts: 12
Try this:
Option ExplicitDim gApp As ObjectPrivate Sub AcrobatFindText()'IAC objectsDim gAvDoc As Object

 '
variablesDim Resp 'For message box responsesDim gPDFPath As String

Dim sText As String '
String to search forDim sStr As String 'Message stringDim foundText As Integer 'Holds return value from "FindText" method 'hard coding for a PDF to open, it can be changed when needed.gPDFPath = "C:\mydocument.pdf" 'Initialize Acrobat by creating App objectSet gApp = CreateObject("AcroExch.App")gApp.Hide 'Set AVDoc objectSet gAvDoc = CreateObject("AcroExch.AVDoc") ' open the PDFIf gAvDoc.Open(gPDFPath, "") ThensText = "factory"'FindText params: StringToSearchFor, caseSensitive (1 or 0), WholeWords (1 or 0), ResetSearchToBeginOfDocument (1 or 0)foundText = gAvDoc.FindText(sText, 1, 0, 1) 'Returns -1 if found, 0 otherwise Else' if failed, show error messageResp = MsgBox("Cannot open" & gPDFPath, vbOKOnly)End If

If foundText = -1 Then

'
compose a messagesStr = "Found " & sTextResp = MsgBox(sStr, vbOKOnly)Else' if failed, show error messageResp = MsgBox("Cannot find" & sText, vbOKOnly)End If

gApp.Show

gAvDoc.BringToFront

End Sub

Checkout the documentation at:
http://www.adobe.com/devnet/acrobat/?tab:downloads=1/

Sincerely,

Jim
Angroberto38
Registered: Feb 26 2008
Posts: 4
Hi I am new and need help on how to launch an Acrobat pdf file from an access database. I have tried to use the CreateObject method but I get an error "429 " ActiveX component can't create object.
Thank you
jdawson422
Registered: Feb 18 2008
Posts: 12
I rarely use VBA in Access. I do a lot in Excel, though. However I just tried this and it worked for me.

To program in VBA in Microsoft Access: Click Tools, Macro, Visual Basic Editor.

Create a module: While in VB editor, click Insert, Module.

Paste the code in the "code pane" window on the right.

Microsoft Access includes command bars, which are programmable toolbars and menu bars. Using command bars, you can create custom toolbars and menus for your application.

In order to program with command bars, you must set a reference to the Microsoft Office object library. Click References on the Tools menu while in module Design view, and select the check box next to Microsoft Office Object Library (on my machine it is "Microsoft Office 11.0 Object Library").

Here is the some code that can get you started.

Option Compare DatabaseOption Explicit

 Dim gApp As Object Sub AcrobatFindText()'IAC objectsDim gAvDoc As Object

 '
variablesDim Resp 'For message box responsesDim gPDFPath As String

Dim sText As String '
String to search forDim sStr As String 'Message stringDim foundText As Integer 'Holds return value from "FindText" method 'hard coding for a PDF to open, it can be changed when needed.gPDFPath = "C:\mydocument.pdf" 'Initialize Acrobat by creating App objectSet gApp = CreateObject("AcroExch.App")gApp.Hide 'Set AVDoc objectSet gAvDoc = CreateObject("AcroExch.AVDoc") ' open the PDFIf gAvDoc.Open(gPDFPath, "") ThensText = "factory"'FindText params: StringToSearchFor, caseSensitive (1 or 0), WholeWords (1 or 0), ResetSearchToBeginOfDocument (1 or 0)foundText = gAvDoc.FindText(sText, 1, 0, 1) 'Returns -1 if found, 0 otherwise Else' if failed, show error messageResp = MsgBox("Cannot open" & gPDFPath, vbOKOnly)End If

If foundText = -1 Then

'
compose a messagesStr = "Found " & sTextResp = MsgBox(sStr, vbOKOnly)Else' if failed, show error messageResp = MsgBox("Cannot find" & sText, vbOKOnly)End If

gApp.Show

gAvDoc.BringToFront

End Sub

Sub AcrobatPageCount()

Dim Resp

'
IAC objectsDim gPDDoc As ObjectDim gAvDoc As Object 'variablesDim gPDFPath As String

Dim rc As Boolean

Dim sStr As String

Dim sName As String

Dim lNum As Integer

 '
hard coding for a PDF to open, it can be changed when needed.gPDFPath = "C:\mydocument.pdf" 'Initialize Acrobat by creating App objectSet gApp = CreateObject("AcroExch.App")gApp.Hide

 '
Set AVDoc objectSet gAvDoc = CreateObject("AcroExch.AVDoc") ' open the PDFIf gAvDoc.Open(gPDFPath, "") Then 'Set PDDoc object and get some dataSet gPDDoc = gAvDoc.GetPDDoc()lNum = gPDDoc.GetNumPages()sName = gPDDoc.GetFileName() 'compose a messagesStr = "PDF file " & sName & " is loaded in Acrobat through IAC program." & vbCrLf & "The PDF document has " & lNum & " pages." & vbCrLf & "The program is over."Resp = MsgBox(sStr, vbOKOnly)

Else

'
if failed, show error messageResp = MsgBox("Cannot open " & gPDFPath & vbCrLf & "The program is over.", vbOKOnly)End If'I have tried various combinations of the lines below and earlier in the routine to get the application to close and/or hide in a predictable way. But no luck.'gApp.ShowgAvDoc.Close (1)'gApp.ExitgApp.Minimize (1)

'
gApp.ShowEnd Sub

 'Create a custom toolbarSub Auto_Open() 'Runs automatically when the file is openedDim MyToolbar As CommandBarDim MyButton As CommandBarButtonDim MyToolbarName As StringDim Resp

 'Give the toolbar a nameMyToolbarName = "PDFtools" ' First, delete the toolbar if it already existsOn Error Resume Next ' so that it doesn't stop on the next line if the toolbar does not existCommandBars(MyToolbarName).Delete ' Build the command barOn Error Resume Next ' so that it doesn't stop on the next line if the toolbar's already thereSet MyToolbar = CommandBars.Add(Name:=MyToolbarName, Position:=msoBarTop) On Error GoTo ErrorHandler

 ' Add a button to the new toolbarSet MyButton = MyToolbar.Controls.Add(Type:=msoControlButton)

 '
MsoButtonStyle constants can be any one of:'msoButtonAutomatic'msoButtonIcon (Button displays Icon only)'msoButtonCaption (Button displays text only)'msoButtonIconAndCaption'msoButtonIconAndCaptionBelow'msoButtonIconAndWrapCaption'msoButtonIconAndWrapCaptionBelow'msoButtonWrapCaption (Read/write Long) ' Set some of the button's propertiesWith MyButton.TooltipText = "Open PDF and find text" 'Tooltip text when mouse is placed over button.Caption = "PDF Find Text" 'Text to be displayed on button.OnAction = "AcrobatFindText" 'Name of routine to run when clicked.Style = msoButtonCaption 'Make button display text onlyEnd With ' Add another button to the new toolbarSet MyButton = MyToolbar.Controls.Add(Type:=msoControlButton)

 '
Set some of the button's propertiesWith MyButton


.TooltipText = "Get number of pages in PDF" '
Tooltip text when mouse is placed over button.Caption = "PDF Page Count" 'Text to be displayed on button.OnAction = "AcrobatPageCount" 'Name of routine to run when clicked.Style = msoButtonCaption 'Make button display text only.BeginGroup = True 'Put a separator line between the buttonsEnd With  MyToolbar.Visible = True On Error GoTo 0 'Resume default error handling NormalExit:

'
MsgBox "Note: This version is still under development. " & _' "Please be careful!"Exit Sub ' so it doesn't go on to run the errorhandler code ErrorHandler:

'
Just in case there is an errorMsgBox Err.Number & vbCrLf & Err.DescriptionResume NormalExit:End Sub

 'Delete the custom toolbar when you close the databaseSub Auto_Close() 'Runs automatically when the file is closedDim bar, MyToolbarName As String ' Specify the toolbar nameMyToolbarName = "PDFtools" ' Delete the toolbarFor Each bar In CommandBarsIf bar.Name = MyToolbarName Thenbar.DeleteEnd IfNext bar

End Sub

Something I find annoying is that the Acrobat application does not close, hide, or show in a logical, predictable way. If anyone can help us here it would be appreciated.

Sincerely

Jim
Angroberto38
Registered: Feb 26 2008
Posts: 4
Jim,
Thank you,
I am with problem in the line Set gApp = CreateObject("AcroExch.App").
I need you help again, i did everything how you posted, but yet it's with problems . I have tried to use the CreateObject method but I get an error "429 " ActiveX component can't create object.
I added references : adobe acrobat 8.0 type library ,microsoft office 12 object library, acrobat access 3.0 type library
Thank you
jdawson422
Registered: Feb 18 2008
Posts: 12
I don't know what is the cause of your error. However, here are all the references that are selected in my VB editor in Access:

Visual basic For Applications
Microsoft Access 11.0 Object Library
OLE Automation
Microsoft DAO 3.6 Object Library
Microsoft ActiveX Data Objects 2.1 Library
Microsoft Office 11.0 Object Library

I think you said you hace Microsoft Access 3.0 Object Library. Perhaps that should be changed to a more recent version. I notice that I have 3.0 available but it is not selected.

Just as a test, I tried to remove "Microsoft Access 11.0 Object Library" and I could not (computer would not allow it, displayed a message telling me it was in use).

Another test, I tried to select the 3.0 version and move its priority above the 11.0 version and it stopped below the 11.0 version and would not move any further.

Also, I noticed that you can change the order (Priority) in which they are listed. Maybe that is important in this situation.

Searching the internet, I found comment for someone using Access 97 that the Dao350.dll file was not registered properly during install of MS Access. Here is the URL to that http://www.mvps.org/access/bugs/bugs0007.htm

I notice that I dont have dao350.dll but instead have dao360.dll. I think either one is OK if it is the proper one for you installation but it must be registered properly.

P.S. This seems to be a Access or system problem and help by posting on a different forum might be more helpful.

Sincerely,

Jim