gpt-4-0314· OpenAI WildChat-4.8M (AI2)
please write a VBA program to batch print excel, word, and ppt files in one folder to be…
0
Download- User
please write a VBA program to batch print excel, word, and ppt files in one folder to be pdf format files.
- Assistant
' First, add references to the Microsoft Word, Microsoft Excel, and Microsoft PowerPoint libraries in VBA by selecting Tools -> References from the menu bar. Sub BatchPrintDocumentsToPDF() ' Declare variables Dim FolderPath As String Dim FileName As String Dim FileExtension As String Dim FullFileName As String ' Set folder path FolderPath = "C:\YourFolderPath\" ' Replace "C:\YourFolderPath\" with the path where your files are. ' Loop through each file in the folder FileName = Dir(FolderPath) Do While FileName <> "" FullFileName = FolderPath & FileName FileExtension = LCase(Right(FileName, Len(FileName) - InStrRev(FileName, ".", -1, vbTextCompare) + 1)) ' Check the file extension/type and trigger the corresponding function Select Case FileExtension Case "xls", "xlsx", "xlsm": PrintExcelToPDF FullFileName Case "doc", "docx": PrintWordToPDF FullFileName Case "ppt", "pptx": PrintPPTToPDF FullFileName End Select FileName = Dir Loop End Sub Sub PrintExcelToPDF(FullFileName As String) ' Declare Excel variables Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim PDFFileName As String ' Create Excel application Set xlApp = New Excel.Application ' Open workbook and get PDF file name Set xlWb = xlApp.Workbooks.Open(FullFileName) PDFFileName = Replace(FullFileName, ".xls", ".pdf") ' Export workbook to PDF xlWb.ExportAsFixedFormat xlTypePDF, PDFFileName ' Close workbook and Quit Excel xlWb.Close SaveChanges:=False xlApp.Quit ' Cleanup Set xlWb = Nothing Set xlApp = Nothing End Sub Sub PrintWordToPDF(FullFileName As String) ' Declare Word variables Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim PDFFileName As String ' Create Word application Set wdApp = New Word.Application ' Open document and get PDF file name Set wdDoc = wdApp.Documents.Open(FullFileName) PDFFileName = Replace(FullFileName, ".doc", ".pdf") ' Export document to PDF wdDoc.ExportAsFixedFormat PDFFileName, wdExportFormatPDF ' Close document and Quit Word wdDoc.Close SaveChanges:=False wdApp.Quit ' Cleanup Set wdDoc = Nothing Set wdApp = Nothing End Sub Sub PrintPPTToPDF(FullFileName As String) ' Declare PowerPoint variables Dim pptApp As PowerPoint.Application Dim pptPres As PowerPoint.Presentation Dim PDFFileName As String ' Create PowerPoint application Set pptApp = New PowerPoint.Application ' Open presentation and get PDF file name Set pptPres = pptApp.Presentations.Open(FullFileName) PDFFileName = Replace(FullFileName, ".ppt", ".pdf") ' Export presentation to PDF pptPres.ExportAsFixedFormat PDFFileName, ppFixedFormatTypePDF ' Close presentation and Quit PowerPoint pptPres.Close pptApp.Quit ' Cleanup Set pptPres = Nothing Set pptApp = Nothing End Sub