如何在Excel中一次性将多个工作簿或工作表转换为PDF文件?
有时,在使用Excel工作时,您需要将Excel工作簿转换为PDF。如果您尝试手动执行此操作,则可能是一个耗时的过程。我们可以使用VBA应用程序来完成此任务,因为它无法直接在Excel中完成。阅读本文以了解如何一次性将多个工作簿或工作表转换为Excel中的PDF文件。让我们以更简短的方式了解该过程。
一次性将多个工作簿转换为Excel中的PDF文件
在这里,我们将首先创建一个VBA模块,然后运行它来选择包含工作簿和PDF的文件夹,然后单击“确定”以完成任务。让我们来看一个简单的过程,了解如何在Excel中一次性将多个工作簿转换为PDF文件。
步骤1
让我们考虑一个新的Excel工作表,然后右键单击工作表名称并选择“查看代码”以打开vba应用程序,然后单击“插入”并选择“模块”。
右键单击 > 查看代码 > 插入 > 模块
然后,如下面的图像所示,将以下程序代码键入文本框。
程序1
Sub ExcelSaveAsPDF() 'Update By Nirmal Dim strPath As String Dim xStrFile1, xStrFile2 As String Dim xWbk As Workbook Dim xSFD, xRFD As FileDialog Dim xSPath As String Dim xRPath, xWBName As String Dim xBol As Boolean Set xSFD = Application.FileDialog(msoFileDialogFolderPicker) With xSFD .Title = "Please select the folder contains the Excel files you want to convert:" .InitialFileName = "C:" End With If xSFD.Show <> -1 Then Exit Sub xSPath = xSFD.SelectedItems.Item(1) Set xRFD = Application.FileDialog(msoFileDialogFolderPicker) With xRFD .Title = "Please select a destination folder to save the converted files:" .InitialFileName = "C:" End With If xRFD.Show <> -1 Then Exit Sub xRPath = xRFD.SelectedItems.Item(1) & "" strPath = xSPath & "" xStrFile1 = Dir(strPath & "*.*") Application.ScreenUpdating = False Application.DisplayAlerts = False Do While xStrFile1 <> "" xBol = False If Right(xStrFile1, 3) = "xls" Then Set xWbk = Workbooks.Open(Filename:=strPath & xStrFile1) xbwname = Replace(xStrFile1, ".xls", "_pdf") xBol = True ElseIf Right(xStrFile1, 4) = "xlsx" Then Set xWbk = Workbooks.Open(Filename:=strPath & xStrFile1) xbwname = Replace(xStrFile1, ".xlsx", "_pdf") xBol = True ElseIf Right(xStrFile1, 4) = "xlsm" Then Set xWbk = Workbooks.Open(Filename:=strPath & xStrFile1) xbwname = Replace(xStrFile1, ".xlsm", "_pdf") xBol = True End If If xBol Then xWbk.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xRPath & xbwname & ".pdf" xWbk.Close SaveChanges:=False End If xStrFile1 = Dir Loop Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub
步骤2
然后将工作表另存为启用宏的工作簿,选择包含Excel文件的文件夹,然后单击“确定”。
步骤3
现在选择要存储PDF文件的文件夹,然后单击“确定”以完成我们的过程。
这就是我们在Excel中一次性将多个工作簿转换为PDF文件的方法。
如果我们需要从单个工作簿中转换多个工作表,则在打开工作簿后使用程序2。
程序2
Sub SplitEachWorksheet() 'Update by Nirmal Dim xSPath As String Dim xSFD As FileDialog Dim xWSs As Sheets Dim xWb As Workbook Dim xWbs As Workbooks Dim xNWb As Workbook Dim xInt, xI As Integer Set xSFD = Application.FileDialog(msoFileDialogFolderPicker) With xSFD .title = "Please select a folder to save the converted files:" .InitialFileName = "C:" End With If xSFD.Show <> -1 Then Exit Sub xSPath = xSFD.SelectedItems.Item(1) Application.ScreenUpdating = False Application.DisplayAlerts = False Set xWb = Application.ActiveWorkbook Set xWbs = Application.Workbooks Set xWSs = xWb.Sheets Set xNWb = xWbs.Add xInt = xWSs.Count For xI = 1 To xInt On Error GoTo EBreak Set xWs = xWSs.Item(xI) If xWs.Visible Then xWSs(xWs.Name).Copy Application.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xSPath & "" & xWs.Name & ".pdf" Application.ActiveWorkbook.Close False End If EBreak: Next xWb.Activate Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub
结论
在本教程中,我们使用一个简单的示例来演示如何在Excel中将多个Excel文件转换为PDF文件。
广告