Batch save files as PDF

Mashtonsmith

Board Regular
Joined
Oct 1, 2004
Messages
164
Sorry to post on an Excel board...

But I need a quick way to save multiple Word.docs as multiple PDF's.

Possible ?

Thank you!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
in vbe (alt-F11) insert a module,
paste this code into it.
set the folder you want to use in,SaveAll2PDF

then run: SaveAll2PDF

Code:
Public Sub SaveAll2PDF()
  SavellFilesInDirAsPDF "c:\temp\"
  MsgBox "Done"
End Sub

'-------------
Private Sub SavellFilesInDirAsPDF(ByVal pvDir)
'-------------
Dim FSO, oFolder, oFile, oRX
Dim sTxt As String, sFile As String
Dim vTargFile
Dim i As Long


Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(pvDir)


If Right(pvDir, 1) = "\" Then pvDir = pvDir & "\"


For Each oFile In oFolder.Files
  If InStr(oFile.Name, ".doc") > 0 Then            'docs only
       sFile = pvDir & oFile.Name
       Documents.Open sFile
       
       i = InStrRev(oFile.Name, ".doc")
       vTargFile = pvDir & Left(oFile.Name, i) & "pdf"
       Save1Pdf vTargFile
       ActiveDocument.Close False
  End If
Next


Set oFile = Nothing
Set oFolder = Nothing
Set FSO = Nothing
End Sub


Private Sub Save1Pdf(ByVal pvFile)

If FileExists(pvFile) Then KillFile (pvFile)

    ActiveDocument.ExportAsFixedFormat OutputFileName:=pvFile, _
         ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub


Public Function FileExists(ByVal pvFile) As Boolean
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FileExists = FSO.FileExists(pvFile)
Set FSO = Nothing
End Function


Public Sub KillFile(ByVal pvFile)
Dim FSO
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
'FileReadOnly pvFile, False
FSO.DeleteFile pvFile
Set FSO = Nothing
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top