Batch convert doc or docx to pdf at specific folder instead of a folder dialog

prati

Board Regular
Joined
Jan 25, 2021
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Hey

Below there is a VBA code to convert multiple word to pdf.

The macro open a folder dialog - i have to select a folder, then the macro open each file in the selected folder and print to pdf.
It's fine, but I want to modify the macro for my own needs.

Does anyone has any knowledge how can i modify the code below so it will look at specific folder C:\Temp instead a folder dialog?


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 & "\*.docx")

While InputWordFile <> ""

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

objWordApp.ActiveDocument.ExportAsFixedFormat OutputFileName:=OutputPdfFile, ExportFormat:=wdExportFormatPDF

objMyWordFile.Close
InputWordFile = Dir
Wend

objWordApp.Documents.Application.Quit

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Substitute:
VBA Code:
'---------------------------------------
'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
'---------------------------------------
with this:
Code:
'input file folder
SelectedWordFilesFolder = "C:\Temp"
 
Upvote 0
Solution
Thank you. It works. Now the macro converts from specific location and not open a folder dialog. Also the code is shorter. Would you please help me with 2 similiar problems?

I'm trying to modify the code in order to convert excel files to pdf, and presenation files to pdf by the same way. Let's start with excel to pdf I changed the code like this,

Sub ConvertMultipleExcelToPDF()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim OpenSourceFolder As Object, OpenTargetFolder As Object
Dim SelectedExcelFilesFolder As String, SelectedPdfFilesFolder As String
Dim InputExcelFile As String, OutputPdfFile As String

Dim objExcelApp As Excel.Application
Dim objMyExcelFile As Excel.Workbook
Set objExcelApp = CreateObject("Excel.Application")


SelectedExcelFilesFolder = "C:\Temp"
SelectedPdfFilesFolder = "C:\Temp"


'Looping through only Excel files in input file folder
InputExcelFile = Dir(SelectedExcelFilesFolder & "\*.xls")

While InputExcelFile <> ""

Set objMyExcelFile = objExcelApp.Workbooks.Open(SelectedExcelFilesFolder & "\" & InputExcelFile)
objExcelApp.Visible = True

If LCase(Right(objMyExcelFile.Name, 1)) = "x" Then
OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyExcelFile.Name, "xlsx", "pdf")
Else
OutputPdfFile = SelectedPdfFilesFolder & "\" & Replace(objMyExcelFile.Name, "xls", "pdf")
End If


objExcelApp.ActiveWorkbook.ExportAsFixedFormat OutputFileName:=OutputPdfFile, ExportFormat:=xlExportFormatPDF

objMyExcelFile.Close
InputExcelFile = Dir
Wend

objExcelApp.Workbooks.Application.Quit

End Sub


I got this error
1612517441921.png
 
Upvote 0
and presenation files to pdf by the same way.
This is a completely different problem, open a new thread in the appropriate section.
As per Excel files your macro should be changed like this:
VBA Code:
    [...]
objExcelApp.ActiveWorkbook.ExportAsFixedFormat Filename:=OutputPdfFile, Type:=xlTypePDF '<= changed
objMyExcelFile.Close False                '<= changed (no saving)
    [...]
 
Last edited:
Upvote 0
Thank you for going above and beyond, you perfectly solved the problems.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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