Highlighted

Search For String In PDF Using VBA

New Here ,
Oct 27, 2020

Copy link to clipboard

Copied

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

 

TOPICS
Acrobat SDK and JavaScript

Views

33

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Search For String In PDF Using VBA

New Here ,
Oct 27, 2020

Copy link to clipboard

Copied

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

 

TOPICS
Acrobat SDK and JavaScript

Views

34

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 27, 2020 0
Adobe Community Professional ,
Oct 27, 2020

Copy link to clipboard

Copied

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. 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 27, 2020 0