Why does the loop in my VBA code stop working after the third go?

129 views Asked by At

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
0

There are 0 answers