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.