PDA

View Full Version : Use ShellExecute to do a programmatic string search in an opened PDF file



pdhudgens
04-16-2015, 02:08 PM
I'm using the following code to successfully open a PDF file using VBA in Excel 2013:


ShellExecute 0, “Open”, MyPDFFile, “”, “”, vbNormalNoFocus

I'm hoping to find a way (via ShellExecute or some other mechanism) to programmatically execute a search within the opened PDF file for a specified string. Does anyone know of a way to do that?

Thanks very much for any help,
Paul Hudgens
Denver

mancubus
04-16-2015, 10:33 PM
hi.

check this googling result:
http://www.myengineeringworld.net/2014/05/pdf-search-through-vba.html

i did not test it.

Kenneth Hobs
04-17-2015, 08:31 AM
The method that mancubus showed and the one here, both requires Adobe Acrobat, not Adobe's Reader.

http://www.vbaexpress.com/kb/getarticle.php?kb_id=1101

Or, a 3rd party program to get the text: http://www.vbaexpress.com/kb/getarticle.php?kb_id=977

Once you get 5 posts, you can probably attach a PDF file for us to test. Not all PDF's can be searched.

pdhudgens
04-21-2015, 09:48 AM
My apologies for my tardy reply - and for not being clearer in my original post. My company has recently switched from Adobe to Foxit - and it is for Foxit that I need to find a way to do programmatic searches via VBA. My many searches for help have turned up nothing specific to Foxit. I'm wondering if SendKeys or SendMessage might be the only answer at this point. My apologies once again and thanks to everyone for their replies.

Paul Hudgens
Denver

Kenneth Hobs
04-21-2015, 10:26 AM
You have looked into the ActiveX SDK? http://cdn01.foxitsoftware.com/pub/foxit/manual/enu/FoxitActiveX24_guide.pdf

pdhudgens
04-21-2015, 02:40 PM
Thanks Kenneth. I have been reading up on ActiveX for Foxit - everything I see says it is a purchased add-on - including the DLL. Is that your understanding? This is needing to be a low budget effort - at least for now.

Kenneth Hobs
04-21-2015, 05:40 PM
While SendKeys() would work, that method should be avoided when possible. In that case, either use SendKeys() or try a 3rd party program as I first suggested. It looks like the referenced one would work. Some 3rd party programs can be used for free or at least free for 30 days and then the cost is minimal.

pdhudgens
04-22-2015, 06:34 AM
Kenneth,

Thanks for the help. If I used the Foxit SDK, or other 3rd party software, wouldn't that software have to be installed on the computers of all future potential users of the program? If so, that would not be feasible.

Meanwhile, I can't even get SendKeys to work. I'm using the following code:


ThisFile = "E:\RigReports\Weekly Rig Report 4-01-15.pdf"
ShellExecute 0, "Open", ThisFile, vbNullString, vbNullString, vbNormalFocus
DoEvents
SendKeys ("^f"), True
Application.Wait (50)
SendKeys (ThisString), True

The file opens fine, but immediately loses focus when the ^f character is sent - so the Find dialog comes up in the Excel workbook rather than in the PDF file. VBNormalFocus is supposed to keep the invoked application (Foxit) in focus, but it doesn't. Do you possibly have any idea why?

Thanks,
Paul Hudgens
Denver

Kenneth Hobs
04-22-2015, 08:04 AM
Now you see why I avoid SendKeys(). Focus and timing are critical. In some cases, some may need UAC turned off. There are API ways to get around the UAC issue.

In any case, try the wait method that I demonstrating in post 5 of: http://www.vbaexpress.com/forum/showthread.php?t=40910

Once the text is copied to the clipboard as I demonstrated in that thread, you can search text that way if needed.

pdhudgens
04-22-2015, 09:40 AM
Kenneth,

That works great! The final code is:


Public Sub RigData1()
Dim ThisFile As String
Dim SrchString As String
SrchString = "COLORADO"
ThisFile = "E:\RigReports\Weekly Rig Report 4-21-15.pdf"
ShellExecute 0, "Open", ThisFile, "", "", vbNormalFocus
Application.Wait (Now + TimeSerial(0, 0, 2))
DoEvents
SendKeys ("^f"), True
SendKeys (SrchString), True
SendKeys ("~"), True
End Sub

I didn't anything with regard to UAC - it apparently is not an issue for Windows 7 Enterprise. Also, not sure that DoEvents is needed - just throwing it in for good measure. Thanks for the help.

Paul Hudgens