Skip to main content
Participant
October 27, 2020
Question

Search For String In PDF Using VBA

  • October 27, 2020
  • 1 reply
  • 4077 views

Hi all,

 

Using Adobe Acrobat Pro DC v 2020.012.20048

I am trying to search pdf files for a string and log the results so I can review the files that return True.

The files are on a \\Server\Share\

 

I tested the code below on a local folder with 3 pdf files and everything worked as expected.
I then changed the path to the \\server\share\ . I did get results for 3 files, then I received an error:

[quote]Run-time error '-2147023170(800706be)':
Automation error
The remote procedure call failed[/quote]

Here:

blnSearch = AVDocObj.findtext(szText:=searchString, _
bCaseSensitive:=False, _
bWholeWordsOnly:=True, _
bReset:=2)

 

Any ideas on what else I can try, or do I have to download all files to local first (64K files)

 

thanks,

-w

 

complete code:

Sub Search_PDF_For_String()
'https://software-solutions-online.com/how-to-search-a-pdf-for-a-string-using-vba-for-excel/

'Objects
Dim wb As Workbook
Dim ws As Worksheet
Dim appObj As Object
Dim AVDocObj As Object
Dim fso As Object
Dim f As Object

'Variables
Dim searchString As String
Dim PDF_path As String
Dim blnSearch As Boolean
Dim r_output As Long

'Initialize objects
Set wb = ThisWorkbook
Set ws = wb.Worksheets("PDF_Search")
Set fso = CreateObject("Scripting.FileSystemObject")

'Excel environment - speed things up
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With

'Initailize variables
r_output = 5

'Get folder path and search phrase
With ws
searchString = .Cells(2, 1).Value
PDF_path = .Cells(1, 1).Value
End With

'Search each file for the phrase
Set fsoFolder = fso.GetFolder(PDF_path)
For Each f In fsoFolder.Files
Set appObj = CreateObject("AcroExch.App")
Set AVDocObj = CreateObject("AcroExch.AVDoc")
'Open the PDF file and check if the open was successful.
If AVDocObj.Open(f.path, "") = True Then
AVDocObj.BringToFront
blnSearch = AVDocObj.findtext(szText:=searchString, _
bCaseSensitive:=False, _
bWholeWordsOnly:=True, _
bReset:=2)
AVDocObj.Close True
appObj.Exit

'Release the objects.
Set AVDocObj = Nothing
Set appObj = Nothing

'log results
With ws
.Cells(r_output, 1).Value = f.path
.Cells(r_output, 2).Value = f.Name
.Cells(r_output, 3).Value = blnSearch
End With
End If
r_output = r_output + 1
Next f

'Add headers
With ws
.Cells(4, 1).Value = "Path"
.Cells(4, 2).Value = "File"
.Cells(4, 3).Value = "Found_T_F"
End With


'Tidy up
'Destroy objects
Set ws = Nothing
Set wb = Nothing

'Excel environment - restore
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With

End Sub

 

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
October 27, 2020

I'm just stabbing in the dark here, but it sounds like a timing issue.  Something in your loop is happening too fast for Acrobat to catch up. Probably realated to opening or closing the PDFs. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often