How to convert PDF files to Excel using sendkeys and microsoft word with VBA.
Here’s the complete VBA code:
Option Explicit
Sub CopyPDFTextToExcel()
Dim varRetVal As Variant, PathToPDF As String, strCommand As String
Sheet1.Cells.ClearContents
Sheet1.Range(“A1”).Activate
‘PathToPDF = “C:UserstakyarDesktoptest1.pdf”
PathToPDF = “C:exceltrainingvideosAboutWindowsAPIs.pdf”
strCommand = “C:Program Files (x86)AdobeAcrobat Reader DCReaderAcroRd32.exe ” & PathToPDF
‘ Use Shell Function to open Adobe Acrobat Reader
varRetVal = Shell(strCommand, 1)
‘ wait
Application.Wait Now + TimeValue(“00:00:05”)
‘First select all data of PDF file, next copy to clipboard
SendKeys “^a” ‘select all data
SendKeys “^c” ‘ copy data
‘Wait
Application.Wait Now + TimeValue(“00:00:05”)
‘Close Acrobat Reader or PDF file
SendKeys “%{F4}”
‘ Wait
Application.Wait Now + TimeValue(“00:00:02”)
‘ Paste to added worksheet (PDFText)
Windows(“pdf-to-excel-using-send-keys.xlsm”).Activate
ActiveSheet.Paste
End Sub
Option Explicit
Function ClearClipboard()
‘Early binding will requires a Reference to ‘Microsoft Forms 2.0 Object Library’
Dim oData As Object ‘New MSForms.DataObject
Set oData = CreateObject(“New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}”)
oData.SetText Text:=Empty
oData.PutInClipboard
Set oData = Nothing
End Function
Sub PDF_To_Excel()
Dim PathToPDFFiles As String
Dim PathToExcelFiles As String
PathToPDFFiles = “C:exceltrainingvideosMyPDFs”
PathToExcelFiles = “C:exceltrainingvideosPDFToExcel”
Dim fso As New FileSystemObject
Dim myFolder As Folder
Dim myFile As File
Set myFolder = fso.GetFolder(PathToPDFFiles)
Dim WordApp As Object
Dim WordDoc As Object
Dim WordRange As Object
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WordApp = CreateObject(“word.application”)
WordApp.Visible = True
Dim nwb As Workbook
Dim nsh As Worksheet
For Each myFile In myFolder.Files
Set WordDoc = WordApp.documents.Open(myFile.Path, False, Format:=”PDF Files”)
Set WordRange = WordDoc.Paragraphs(1).Range
WordRange.WholeStory
Set nwb = Workbooks.Add
Set nsh = nwb.Sheets(1)
WordRange.Copy
nsh.Paste
nwb.SaveAs (PathToExcelFiles & Replace(myFile.Name, “.pdf”, “.xlsx”))
Application.CutCopyMode = False
Call ClearClipboard
WordDoc.Close True
nwb.Close True
Next
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox “Conversion complete!”
End Sub
All details also available at this web-link: …(read more)
Convert Word 2 PDF: Word to PDF Converter
Convert PowerPoint 2 PDF: PPT to PDF Converter
Convert Excel 2 PDF: Excel to PDF Converter
Convert an Image 2 PDF: Image to PDF Converter
Convert HTML 2 PDF: HTML to PDF Converter
More Tools: PDF Converter
for 2nd step, i have an error. when i click run the excel file will crash. i didn't get the same way as yours. what happens?
Thanks very much sir for the information videos
Great code, but Adobe Reader throws an error complaining that it cannot find my files. I have tried several, in different locations. I am sure the paths are correct and that the files are named correctly. I am using the most up-to-date version as of today. Continuous Release Ver 2021.005.20048 / File Version 21.005.20048.43252.
Do you have any suggestions? Thanks!
Hello sir
Thanks for sharing! It's really helpful
I am unable to convert specific pdf to excel using this program will you please guide on this?
Sir, many thanks to you my friend. I will definitely add the code to my bag of s/w tricks. Again, thanks
Thank you for this video tutorial
Sir I need to paste into one excel as transpose. Can I get the code alone?
Hi Dinesh, Excellent work! I have a question. Is that possible to copy the content including the content of the footer ?
Sir it's not working for me.. means not in good format.. please help me out on this
Why do u declare wordapp object? Do we add library to connect with word? If yes then which one
Macro is working but output is not in text its in object…..Not in all pdf in few pdf it showing Object
Hello Sir, I enjoyed all your very helpful videos. May I ask if there is any chance to assign the windows navigation start sound to a button in excel when it is clicked. Thanking you in advance.
I am getting an error in the last line of macro " windows ("pdf-to-excel using send keys.xlsm").Activate" its getting highlighted in yellow what is this plz help
What you did is great, creative. All respect for you from Egypt
hi sir, thank you for your educational videos it has really helped me to make a small application for my school but right now i've one issue if you could help me with that it will really help my school a lot. – i have a form made using VBA, now before saving that data to the database i would like to print the same data using a pre-formatted page in excel how can i do that? the data from the VBA textbox should be assigned to the activex textbox in a different excel sheet of the same workbook. please let me know on this thanks.
Anyone can add Chinese subtitle? Could you help share your code to me?
Sir,
Macro is working but output is not
readable and format also not match with original pdf file.
Thank you very much Sir.
Not Worked In MS Office 2007
Thank for the video,
Sir can share VBA code for combine or merge multiple PDF files
How i use late binding of FileSystemObject in this example.
I have a problem on this line "nsh.Paste"
It will be for sure in my library. Thank you Sir
Thanks Dinesh !
Definitely a new addition to my Excel VBA toolkit! Thanks
Thanks Dinesh