Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-18-2017, 03:52 AM
ballpoint ballpoint is offline Looping through a folder of PDF files and saving them as docx Windows 10 Looping through a folder of PDF files and saving them as docx Office 2016
Advanced Beginner
Looping through a folder of PDF files and saving them as docx
 
Join Date: Sep 2017
Posts: 42
ballpoint is on a distinguished road
Default 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

Reply With Quote
  #2  
Old 11-18-2017, 05:52 AM
gmayor's Avatar
gmayor gmayor is offline Looping through a folder of PDF files and saving them as docx Windows 10 Looping through a folder of PDF files and saving them as docx Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 11-18-2017, 06:36 AM
ballpoint ballpoint is offline Looping through a folder of PDF files and saving them as docx Windows 10 Looping through a folder of PDF files and saving them as docx Office 2016
Advanced Beginner
Looping through a folder of PDF files and saving them as docx
 
Join Date: Sep 2017
Posts: 42
ballpoint is on a distinguished road
Default

Oh, fudge! You are right. Thanks a lot, this is really helpful!
Reply With Quote
  #4  
Old 11-15-2018, 03:38 AM
ballpoint ballpoint is offline Looping through a folder of PDF files and saving them as docx Windows 10 Looping through a folder of PDF files and saving them as docx Office 2016
Advanced Beginner
Looping through a folder of PDF files and saving them as docx
 
Join Date: Sep 2017
Posts: 42
ballpoint is on a distinguished road
Default

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
Does anybody have an idea why that might be? Thanks!
Reply With Quote
  #5  
Old 11-15-2018, 05:47 PM
Guessed's Avatar
Guessed Guessed is offline Looping through a folder of PDF files and saving them as docx Windows 10 Looping through a folder of PDF files and saving them as docx Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #6  
Old 11-22-2018, 05:18 AM
ballpoint ballpoint is offline Looping through a folder of PDF files and saving them as docx Windows 10 Looping through a folder of PDF files and saving them as docx Office 2016
Advanced Beginner
Looping through a folder of PDF files and saving them as docx
 
Join Date: Sep 2017
Posts: 42
ballpoint is on a distinguished road
Default

Oooh, you are correct. Sorry about it! Thanks a lot!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through a folder of PDF files and saving them as docx Run Code on all files and save files as .docx Plokimu77 Word VBA 4 06-05-2016 04:41 PM
Looping through a folder of PDF files and saving them as docx 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
Looping through a folder of PDF files and saving them as docx Word Mac saving as PDF not docx Gweilo Word 3 02-16-2015 03:38 PM
Looping through a folder of PDF files and saving them as docx Problem saving docx files in Win 7 donkrafft Word 12 02-05-2010 05:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:04 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft