#1
|
|||
|
|||
Looping through a folder of PDF files and saving them as docx
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 |
#2
|
||||
|
||||
The fact that you are saving the documents as PDF format may have something to do with it.
Try the following instead. I don't think you can dismiss the conversion prompt in VBA. You can dismiss it from the message itself. Code:
Option Explicit Sub UpdateDocuments() Application.ScreenUpdating = False Dim strFolder As String, strFile As String, wdDoc As Document Dim strDocName As String strFolder = GetFolder If strFolder = "" Then Exit Sub strFile = Dir$(strFolder & "\*.pdf") While strFile <> "" Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False) strDocName = Replace(strFile, ".pdf", ".docx") wdDoc.SaveAs2 FileName:=strFolder & "\" & strDocName, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False wdDoc.Close SaveChanges:=wdDoNotSaveChanges strFile = Dir$() Wend Set wdDoc = Nothing Application.ScreenUpdating = True End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Oh, fudge! You are right. Thanks a lot, this is really helpful!
|
#4
|
|||
|
|||
Hi all,
I was reusing this macro after some time. For some reason, it now highlights "GetFolder" and says: Code:
Compile Error Variable not defined |
#5
|
||||
|
||||
GetFolder is a function that is being called (ie another macro that needs to be in that file). Perhaps you deleted it or moved the macro into another template and forgot to bring GetFolder over as well.
If you have a look at the original code posted at the top of this thread, you can see this macro sitting there.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
Oooh, you are correct. Sorry about it! Thanks a lot!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Run Code on all files and save files as .docx | Plokimu77 | Word VBA | 4 | 06-05-2016 04:41 PM |
Macro to change all text color to black in all docx files in a selected folder | joewoods | Word VBA | 13 | 05-16-2016 06:29 PM |
Saving PPT as a looping video | Frank Bugek | PowerPoint | 0 | 08-23-2015 08:04 PM |
Word Mac saving as PDF not docx | Gweilo | Word | 3 | 02-16-2015 03:38 PM |
Problem saving docx files in Win 7 | donkrafft | Word | 12 | 02-05-2010 05:15 PM |