0

So what I am trying to do is go through a PDF, send key for CTRL+F to find a string of text from column C and highlight the first result, then another send key to bookmark the word. When I try looping, it succeeds for the first 3, but fails. I am using Foxit, and I can't seem to find a better alternative to the sendcodes, so I am open to recommendations as well. I have looked through the API Foxit guide, and have not found any codes specific to what I need.

Sub BookmarkingWithSendKeys()

ActiveWorkbook.FollowHyperlink "C:\Test.pdf"

    Dim searchString As String
    'Create loop using variable i, searching PDF in column C (3 if you do R1C1).
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    For I = 1 To FinalRow

    searchString = Cells(I, 3).Value

    'Send "CTRL + F" keys to the opened PDF to invoke find within that file
    'The second argument "true" implies that Excel will wait for the keys to be processed before returning control to the macro.
    SendKeys "^f", True

    'Type the search string in the find box that opens
    SendKeys searchString, True

    'Hit enter to perform the search
    SendKeys "{Enter}", True

    'Bookmark highlighted text
    SendKeys "^b", True

    'Hit enter to perform the bookmark
    SendKeys "{Enter}", True
        Next I

    Application.DisplayAlerts = True

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
  • 2
    `SendKeys` is almost impossible to reliably debug. A common kludge is to introduce little pauses of e.g. 0.5 seconds between successive `SendKeys` invocations. But -- even if you get it to "work" it is likely to break sooner or later. You are essentially typing blind, not even being 100% sure what window you are typing to. – John Coleman Nov 26 '19 at 23:38
  • 1
    I dont even bother with `SendKeys` - If that's the only way to communicate with something then it's not worth the time. – braX Nov 27 '19 at 00:00
  • Like the others said, `SendKeys` is a recipe for disaster. Since you are using Excel, you probably also have Word. So what you could do is open the PDF in Word (it will convert the file to a Word-read/writeable format), then save/export the content as a text file, then use that as input (or even talk to Word directly). This would be *much* less error-prone. I haven't tried it beyond just recording a macro (in Word) to see what happens if you open a PDF in it and that seems to work fine. I use Office 2019 btw. – Rno Nov 27 '19 at 00:40
  • Links that may interest you: https://stackoverflow.com/questions/36270247/extract-data-from-pdf-and-add-to-worksheet and https://stackoverflow.com/questions/3567441/extract-data-from-word-document-to-an-excel-spreadsheet – Rno Nov 27 '19 at 00:42
  • Might be possible that the foxit thing needs some time to perform your actions. I would try to put some delays between the send key calls. – Thomas Ludewig Nov 27 '19 at 01:29

0 Answers0