I am trying to convert pdf files into excel files. And converter is working perfect, but when it gets big pdf with like 300 pages "microsoft excel is waiting for another application to complete an ole action" error appears.
Here is the code:
Option Explicit
Sub PDF_To_Excel()
Dim setting_sh As Worksheet
Set setting_sh = ThisWorkbook.Sheets("Setting")
Dim pdf_path As String
Dim excel_path As String
pdf_path = setting_sh.Range("E11").Value
excel_path = setting_sh.Range("E12").Value
Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
Set fo = fso.GetFolder(pdf_path)
Dim wa As Object
Dim doc As Object
Dim wr As Object
Set wa = CreateObject("word.application")
wa.Visible = True
Dim nwb As Workbook
Dim nsh As Worksheet
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 False
nwb.Close False
Next
wa.Quit
MsgBox "Done"
End Sub
I've tried to do something with settings and it didn't help. Application.wait and Application.DisplayAlerts = False didn't help also.
Is there anything that I can do or these files just too big?
I have not tested this but here are som ideas....
Working with large PDF files and converting them to Excel can be resource-intensive, and it seems like you're encountering issues with the current approach. Here are a few suggestions that might help:
Batch Processing: Instead of processing all 300 pages at once, consider processing the PDF in smaller chunks or pages at a time. You can modify your loop to handle a specific range of pages in each iteration.
Memory Management: Large PDF files can consume a lot of memory. Ensure that you release objects and manage memory properly. Set objects to Nothing when you're done with them.
Error Handling: Implement proper error handling to identify the exact location of the problem. Add On Error Resume Next and check for errors after key statements.