I have recently realised that a large number of PDFs can be opened within Word on Windows retaining the formatting and the footnotes. Accordingly, I thought I'd try it on a collection of files I have. Problem is, I cannot seem to loop through the PDF files in a folder and saving them as .docx.
I have managed to get it to open the files, but not to save them as .docx. Ideally, I would also need some insight as to how to bypass error messages and proceed to the next file when that happens, but I have not been able to find a way to do that either.
I must have made some major mistakes on the way, but below is what I have tried. Many thanks!
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.pdf", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatPDF
ActiveWindow.Close SaveChanges = False
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function