VBA Batch convert to PDF

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have a macro that batch converts word documents to PDF. It works, but sometimes the word documents are in both .doc and .docx. Is there a way to alter the code so it will do both extensions? I tried to alter the code but it was changing .doc to PDF and .docx to PDFX. Any help would be appreciated. Below is the code I am using.

Code:
Sub ConvertMultipleWordToPDF()
 
Application.ScreenUpdating = False
Application.DisplayAlerts = False
 
Dim OpenSourceFolder As Object, OpenTargetFolder As Object
Dim SelectedWordFilesFolder As String, SelectedPdfFilesFolder As String
Dim InputWordFile As String, OutputPdfFile As String
 
 
Dim objWordApp As Word.Application
Dim objMyWordFile As Word.Document
Set objWordApp = CreateObject("Word.Application")
 
 
Set OpenSourceFolder = Application.FileDialog(msoFileDialogFolderPicker)
Set OpenTargetFolder = Application.FileDialog(msoFileDialogFolderPicker)
 
'Select input file folder
MsgBox ("Select input folder where word files are stored")
Set OpenSourceFolder = Application.FileDialog(msoFileDialogFolderPicker)
 
If OpenSourceFolder.Show = -1 Then
SelectedWordFilesFolder = OpenSourceFolder.SelectedItems(1)
End If
 
 
If SelectedWordFilesFolder = "" Then
MsgBox "No input folder selected, code will exit"
Exit Sub
End If
 
 
AppActivate Application.Caption
 
'Select output file folder
MsgBox ("Select output folder where PDF files are stored")
If OpenTargetFolder.Show = -1 Then
SelectedPdfFilesFolder = OpenTargetFolder.SelectedItems(1)
End If
 
 
If SelectedPdfFilesFolder = "" Then
MsgBox "No output folder selected, code will exit"
Exit Sub
End If
 
 
 
'Looping through only word files in input file folder
InputWordFile = Dir(SelectedWordFilesFolder & "\*.doc")

While InputWordFile <> ""
          
Set objMyWordFile = objWordApp.Documents.Open(SelectedWordFilesFolder & "\" & InputWordFile)
objWordApp.Visible = True
OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "doc", "pdf")

objWordApp.ActiveDocument.ExportAsFixedFormat OutputFileName:=OutputPdfFile, ExportFormat:=wdExportFormatPDF
      
objMyWordFile.Close
InputWordFile = Dir
Wend
 
objWordApp.Documents.Application.Quit
 
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Change this line:
OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "doc", "pdf")

For this:
OutputPdfFile = SelectedPdfFilesFolder & "\" & Left(objMyWordFile.Name, InStrRev(objMyWordFile.Name, ".") - 1) & ".pdf"
 
Upvote 0
Change this line:
OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "doc", "pdf")

For this:
OutputPdfFile = SelectedPdfFilesFolder & "\" & Left(objMyWordFile.Name, InStrRev(objMyWordFile.Name, ".") - 1) & ".pdf"
I tried this but it didn't work. The .docx were saved as PDF but the .doc files it skipped and they weren't saved.
 
Upvote 0
Try this
Change this line:
OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "doc", "pdf")

For these
VBA Code:
  If LCase(Right(objMyWordFile.Name, 1)) = "x" Then
    OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "docx", "pdf")
  Else
    OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "doc", "pdf")
  End If
 
Upvote 0
Try this
Change this line:
OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "doc", "pdf")

For these
VBA Code:
  If LCase(Right(objMyWordFile.Name, 1)) = "x" Then
    OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "docx", "pdf")
  Else
    OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyWordFile.Name, "doc", "pdf")
  End If
This worked but I had to add another line InputWordFile = Dir(SelectedWordFilesFolder & "\*.doc")
 
Upvote 0
Do you have the problem on this line?

VBA Code:
Dim objWordApp As Word.Application

In the VBA menu select the Tools / Reference option and mark the reference:
Microsoft Word nn.0 Object Library

Note: nn is the version of your office
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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