Hi all, I am currently trying to create a macro that helps me convert about 150 PDF files in a folder to Excel files in another folder. Below is my code:
Sub PDF_To_Excel()
Dim ws As Worksheet, pdf_path As String, excel_path As String
Set ws = ThisWorkbook.Sheets("Sheet 1")
pdf_path = "D:\Test\PDF"
excel_path = "D:\Test\Excel"
Dim fso As New FileSystemObject, fo As Folder, f As File, wa As Object, doc As Object, wr As Object, nwb As Workbook, nsh As Worksheet
Set fo = fso.GetFolder(pdf_path)
Set wa = CreateObject("word.application")
wa.Visible = False
For Each f In fo.Files
Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
Set wr = doc.Paragraphs(1).Range
wr.WholeStory
Set nwb = Workbooks.Add
Set nsh = nwb.Sheets(1)
wr.Copy
nsh.Paste
nwb.SaveAs (excel_path & Replace(f.Name, ".pdf", ".xlsx"))
doc.Close
nwb.Close
Next
wa.Quit
End If
End Sub
After running this macro, the error either is "Error 400"or "Runtime Error '1004': Application-defined or Object-defined error".
Anyone has any idea to debug this? I have tried looking for few days but still can't figure it out.
Thanks in advance.
Sub PDF_To_Excel()
Dim ws As Worksheet, pdf_path As String, excel_path As String
Set ws = ThisWorkbook.Sheets("Sheet 1")
pdf_path = "D:\Test\PDF"
excel_path = "D:\Test\Excel"
Dim fso As New FileSystemObject, fo As Folder, f As File, wa As Object, doc As Object, wr As Object, nwb As Workbook, nsh As Worksheet
Set fo = fso.GetFolder(pdf_path)
Set wa = CreateObject("word.application")
wa.Visible = False
For Each f In fo.Files
Set doc = wa.documents.Open(f.Path, False, Format:="PDF Files")
Set wr = doc.Paragraphs(1).Range
wr.WholeStory
Set nwb = Workbooks.Add
Set nsh = nwb.Sheets(1)
wr.Copy
nsh.Paste
nwb.SaveAs (excel_path & Replace(f.Name, ".pdf", ".xlsx"))
doc.Close
nwb.Close
Next
wa.Quit
End If
End Sub
After running this macro, the error either is "Error 400"or "Runtime Error '1004': Application-defined or Object-defined error".
Anyone has any idea to debug this? I have tried looking for few days but still can't figure it out.
Thanks in advance.