如何在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檔案。

更新於:2023年3月6日

738 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告