【问题标题】:Save each worksheet from workbook as individual pdfs将工作簿中的每个工作表另存为单独的 pdf
【发布时间】:2017-09-14 20:28:01
【问题描述】:

我拥有的是一个工作簿,其中包含“Sheet”中所有销售人员的所有销售,而在其他工作表中,这些工作表由销售人员编号(“41”、“51”、“88”、等)与他们的销售。我想要宏做的是获取每个工作表并保存为带有“工作表名称”和“文件名”的 pdf

我的问题与这篇文章有关,但由于某种原因,我的版本没有正确保存 pdf。

excel vba - save each worksheet in workbook as an individual pdf

所以我想要的很简单:获取每个工作表并保存到它自己独特的 pdf 中。我遇到的问题是宏正在使用正确的文件名保存每个单独的工作表,但是当我打开 pdf 时,每个 pdf 的销售助理都是相同的。

代码如下:

Option Explicit

Sub WorksheetLoop()

Dim wsA     As Worksheet
Dim wbA     As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile  As Variant
Dim WS_Count As Integer
Dim I       As Integer

' Set WS_Count equal to the number of worksheets in the active workbook.
Set wbA = ActiveWorkbook
WS_Count = wbA.Worksheets.Count
strPath = wbA.Path
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
    strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

' Begin the loop.
For I = 1 To WS_Count

    'replace spaces and periods in sheet name
    strName = Replace(wbA.Worksheets(I).Name, " ", "")
    strName = Replace(strName, ".", "_")

    'create default name for savng file
    strFile = strName & "_" & strTime & ".pdf"
    myFile = strPath & strFile

    Debug.Print myFile

    'export to PDF if a folder was selected
    If myFile <> "False" Then
        ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=myFile, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False
        'confirmation message with file info
        MsgBox "PDF file has been created: " _
               & vbCrLf _
               & myFile
    End If

Next I

End Sub

如果您需要任何其他详细信息,请告诉我

【问题讨论】:

  • 销售助理的信息从何而来?

标签: vba excel pdf


【解决方案1】:

您需要激活Activate 每个工作表,然后才能将它们打印成 pdf。试试这个

 ' Begin the loop.
     For Each wsA In wbA.Sheets

        wsA.Activate
        'replace spaces and periods in sheet name
        strName = Replace(wsA.Name, " ", "")
        strName = Replace(strName, ".", "_")

        'create default name for savng file
        strFile = strName & "_" & strTime & ".pdf"
        myFile = strPath & strFile

        Debug.Print myFile

        'export to PDF if a folder was selected
        If myFile <> "False" Then
             ActiveSheet.ExportAsFixedFormat _
                        Type:=xlTypePDF, _
                        Filename:=myFile, _
                        Quality:=xlQualityStandard, _
                        IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, _
                        OpenAfterPublish:=False
            'confirmation message with file info
            MsgBox "PDF file has been created: " _
              & vbCrLf _
              & myFile

        End If

     Next

【讨论】:

    【解决方案2】:

    您应该先激活每张工作表,然后再将其导出为 PDF。试试:

    Option Explicit
    
    Sub WorksheetLoop()
    
    Dim wsA     As Worksheet
    Dim wbA     As Workbook
    Dim strTime As String
    Dim strName As String
    Dim strPath As String
    Dim strFile As String
    Dim strPathFile As String
    Dim myFile  As Variant
    Dim WS_Count As Integer
    Dim I       As Integer
    
    ' Set WS_Count equal to the number of worksheets in the active workbook.
    Set wbA = ActiveWorkbook
    WS_Count = wbA.Worksheets.Count
    strPath = wbA.Path
    strTime = Format(Now(), "yyyymmdd\_hhmm")
    
    'get active workbook folder, if saved
    strPath = wbA.Path
    If strPath = "" Then
        strPath = Application.DefaultFilePath
    End If
    strPath = strPath & "\"
    
    ' Begin the loop.
    For Each wsA In wbA.Worksheets
        wsA.Activate
        'replace spaces and periods in sheet name
        strName = Replace(wsA.Name, " ", "")
        strName = Replace(strName, ".", "_")
    
        'create default name for savng file
        strFile = strName & "_" & strTime & ".pdf"
        myFile = strPath & strFile
    
        Debug.Print myFile
    
        'export to PDF if a folder was selected
        If myFile <> "False" Then
            ActiveSheet.ExportAsFixedFormat _
                    Type:=xlTypePDF, _
                    Filename:=myFile, _
                    Quality:=xlQualityStandard, _
                    IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, _
                    OpenAfterPublish:=False
            'confirmation message with file info
            MsgBox "PDF file has been created: " _
                   & vbCrLf _
                   & myFile
        End If
    
    Next wsA
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多