2

I want to read a PDF into Excel using VBA. I've been able to do so using sendkeys, but this is not a reliable solution as sendkeys are generally vulnerable. Is there any other method or any direct method (In VBA or Python)?

Code which uses sendkeys:

Sub Get_Pdf()
    Dim XLName As String, PDFPath As String, READERPath As String
    Dim OpenPDF, sh As Worksheet

    XLName = ThisWorkbook.Name
    Set sh = ThisWorkbook.Sheets(1)
    PDFPath = Application.GetOpenFilename(filefilter:="PDF file (*.pdf), *.pdf")

    If UCase(PDFPath) = "FALSE" Then Exit Sub
    '~~> Below path differs depending Adobe version and installation path
    READERPath = "C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"
    Shell READERPath & " " & PDFPath, vbNormalFocus: DoEvents

    Application.Wait Now + TimeValue("00:00:2")

    SendKeys "^a", True
    Application.Wait Now + TimeValue("00:00:2")

    SendKeys "^c"
    Application.Wait Now + TimeValue("00:00:2")

    Windows(XLName).Activate
    sh.Paste sh.Range("A1")
    SendKeys "%{F4}", True
End Sub

I researched a method to copy and paste using a method named "copy to clipboard". However, no code or anything was available. If anyone can help regarding this also, it'll be appreciated.

Also, I found a method which does not uses sendkeys. It actually defines a function which uses virtual keyboard. But apparently, that method is also not working as the code is simply not running/giving the output.

Code:

Function F4()
    keybd_event VK_F4, 1, 0, 0
End Function

We can directly define the same other function and directly call these functions instead of senkeys. But no output.

If anyone can help with any of the aforementioned methods, it will be a great help.

Prabal
  • 51
  • 1
  • 6
  • 1
    Please do not post [the same question](https://stackoverflow.com/questions/44073660/reading-pdf-into-excel-without-sendkeys?rq=1) multiple time. Instead, [edit] your post and add details to it instead. (You should at least go back and delete the other post.) – Ken White May 22 '17 at 12:53
  • 1
    already asked and answered? https://stackoverflow.com/questions/36270247/vba-extract-data-from-pdf-and-add-to-worksheet – John Muggins May 22 '17 at 12:55
  • Several ideas [here](https://social.msdn.microsoft.com/Forums/en-US/b2264226-da8e-4453-8f02-3fa3773d090b/how-read-pdf-file-using-vba?forum=isvvba) – Thomas G May 22 '17 at 13:03
  • I speak from experience that this method of doing this may not function correctly. In vba or Python, you may want to research a PDF library. iText is a good one. – Jimmy Smith May 22 '17 at 13:54

0 Answers0