Converting a PDF file to a Word doc is very common tasks I see in work places and freelance sites. And in this Word VBA tutorial, I will be showing you how we can create a Word Macro using VBA to perform a mass PDF files to Word docs conversion task.
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Source Code:
Sub Main() Dim Source_Folder_Path As String, Target_Folder_Path As String Dim File_Names As String Dim doc As Document '// Step 1. Assign Folder Paths Source_Folder_Path = "<Source Folder Path>" Target_Folder_Path = "<Target Folder Path>" If Right(Source_Folder_Path, 1) <> "\" Then Source_Folder_Path = Source_Folder_Path & "\" End If If Right(Target_Folder_Path, 1) <> "\" Then Target_Folder_Path = Target_Folder_Path & "\" End If '// Step 2. Grad all the PDF files File_Names = Dir(Source_Folder_Path & "*.pdf") Application.DisplayAlerts = wdAlertsNone Do While File_Names <> "" Set doc = Documents.Open(Source_Folder_Path & File_Names, False) '// Convert the PDF file to Word Doc doc.SaveAs2 Target_Folder_Path & Replace(File_Names, ".pdf", ".docx"), wdFormatDocumentDefault doc.Close False Set doc = Nothing File_Names = Dir() Loop Application.DisplayAlerts = wdAlertsAll MsgBox "Conversion is finished" End Sub
Will give it a try next week. May need to tinker with so adds pdfs to active docx if page total is less than 11, to fit into an existing Customer evidence compression suite of Macros. I have sorted Macros to deal with Outlook to Docx, and to resize embedded images and attach image attachments. Pdfs is a non standard VBA problem I haven’t resolved yet.
This is brilliant, works really quick on my new work laptop as a test.
It works with the source and target folder if they are both the same.
You can see I have switched off the pop ups, and I tell the system pop up that can be clicked “don’t show this again”, so one click is possible via a shortcut link in the ribbon.
I use very sensitive client pdfs so, not sure if I can use this, as changes to the original is not acceptable in a conversion.
I like the fact that this does not use Adobe Reference items, I have tried some samples online that do this but I have not got them to work, and I guess files go online, I am not really sure, but if they do this is not compliant with DPA and GDPR 2018. Would be interested to see how these compare, I have only looked at VBAa2z on YouTube but this seems overtly complicated.
I will look at this VBA again to see if I can fit it in to the existing automation I am doing, and buy you a coffee or two…
Sub PdftoDocx()
‘Source: https://learndataanalysis.org/create-a-macro-in-ms-word-to-convert-pdf-files-to-word-docs-word-vba/
Dim Source_Folder_Path As String, Target_Folder_Path As String
Dim File_Names As String
Dim doc As Document
‘// Step 1. Assign Folder Paths
Source_Folder_Path = “C:\mn\attach\”
Target_Folder_Path = “C:\mn\attach\PdftoDocx”
If Right(Source_Folder_Path, 1) “\” Then
Source_Folder_Path = Source_Folder_Path & “\”
End If
If Right(Target_Folder_Path, 1) “\” Then
Target_Folder_Path = Target_Folder_Path & “\”
End If
‘// Step 2. Grad all the PDF files
File_Names = Dir(Source_Folder_Path & “*.pdf”)
”Application.DisplayAlerts = wdAlertsNone ”(Switch off 25/10/2021)
Do While File_Names “”
Set doc = Documents.Open(Source_Folder_Path & File_Names, False)
‘// Convert the PDF file to Word Doc
doc.SaveAs2 Target_Folder_Path & Replace(File_Names, “.pdf”, “.docx”), wdFormatDocumentDefault
doc.Close False
Set doc = Nothing
File_Names = Dir()
Loop
”Application.DisplayAlerts = wdAlertsAll ”(Switched off 25/10/2021)
”MsgBox “Conversion is finished” ”(Switched off 25/10/2021)
End Sub
If there are furthering PDF processings required, VBA is really not the right tool. But if you just want to convert PDFs to Word, Microsoft has improved its backend engine greatly to the point the conversion is almost flawless – in my personal opinion.