どうせ誰も読んでない( *゚∀゚)v―.o.oo

だれも読んでないと思って勝手なことを!

Excel VBAで指定フォルダから複数のパワーポイントファイルを選択し、それらを白黒PDFで印刷する

Office 365に対応しています。

 

 

Sub PrintPowerPointToPDF()

Dim ppApp As Object ' PowerPoint Application
Dim ppPres As Object ' PowerPoint Presentation
Dim ppSlide As Object ' PowerPoint Slide
Dim fileDialog As Object ' FileDialog object
Dim selectedFiles As Variant ' Selected files array
Dim filePath As String ' File path
Dim fileExtension As String ' File extension
Dim pdfPath As String ' PDF file path
Dim printColor As Boolean ' Print in color or not

' Create a FileDialog object as a File Picker dialog box.
Set fileDialog = Application.FileDialog(msoFileDialogOpen)

' Allow multiple file selection.
fileDialog.AllowMultiSelect = True

' Set the title of the dialog box.
fileDialog.Title = "Select PowerPoint Files"

' Set the filters to show only PowerPoint files.
fileDialog.Filters.Clear
fileDialog.Filters.Add "PowerPoint Files", "*.pptx; *.pptm; *.ppt; *.ppsx; *.ppsm; *.pps", 1

' Show the dialog box. If the user cancels, exit the macro.
If fileDialog.Show <> -1 Then
Exit Sub
End If

' Get the selected files and loop through them.
selectedFiles = fileDialog.SelectedItems
For Each filePath In selectedFiles
' Check the file extension to ensure it's a PowerPoint file.
fileExtension = UCase(Right(filePath, Len(filePath) - InStrRev(filePath, ".")))
If fileExtension Like ".PPT*" Then
' Open the PowerPoint file.
Set ppApp = CreateObject("PowerPoint.Application")
Set ppPres = ppApp.Presentations.Open(filePath)
ppApp.Visible = True

' Print the PowerPoint file to PDF in black and white.
printColor = False ' Set to True for color printing.
pdfPath = Replace(filePath, fileExtension, ".pdf", , , vbTextCompare)
ppPres.ExportAsFixedFormat pdfPath, ppFixedFormatTypePDF, ppPrintHandoutHorizontalFirst, ppPrintAll, ppPrintHandoutHorizontalFirst, ppPrintBlackAndWhite, ppPrintHighQualityPrint

' Close the PowerPoint file.
ppPres.Close
ppApp.Quit
Set ppPres = Nothing
Set ppApp = Nothing
End If
Next filePath

' Display a message when the PDF printing is complete.
MsgBox "PDF printing complete."

End Sub

 

 

このコードでは、PowerPointファイルを開いて、ExportAsFixedFormatメソッドを使用してPDFに印刷します。白黒印刷をするために、ppPrintBlackAndWhiteをppPrintColorに変更することで、カラー印刷を実行できます。