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
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?
VBA Batch convert to PDF
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...
www.mrexcel.com
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