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文件。