Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将工作簿中的每个工作表保存为单独的PDF_Vba_Excel_Pdf - Fatal编程技术网

Vba 将工作簿中的每个工作表保存为单独的PDF

Vba 将工作簿中的每个工作表保存为单独的PDF,vba,excel,pdf,Vba,Excel,Pdf,我所拥有的是一份工作簿,其中所有销售人员的所有销售记录都在“工作表”中,而在其他工作表中,工作表是以销售人员编号(“41”、“51”、“88”等)命名的。我希望宏执行的是将每个工作表保存为带有“工作表名称”和“文件名”的pdf格式 我的问题与这篇文章有关,但由于某些原因,我的版本没有正确保存pdf 所以我想要的很简单:将每个工作表保存到它自己独特的pdf中。我遇到的问题是,宏使用正确的文件名保存每个工作表,但当我打开pdf时,每个pdf的销售助理都是相同的 代码如下: Option Expli

我所拥有的是一份工作簿,其中所有销售人员的所有销售记录都在“工作表”中,而在其他工作表中,工作表是以销售人员编号(“41”、“51”、“88”等)命名的。我希望宏执行的是将每个工作表保存为带有“工作表名称”和“文件名”的pdf格式

我的问题与这篇文章有关,但由于某些原因,我的版本没有正确保存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
选项显式
子工作表loop()
将wsA设置为工作表
将wbA设置为工作簿
作为字符串的Dim strTime
将strName设置为字符串
将strPath设置为字符串
作为字符串的Dim strFile
将strPathFile设置为字符串
Dim myFile作为变量
Dim WS_计数为整数
作为整数的Dim I
'将WS_Count设置为活动工作簿中的工作表数。
设置wbA=ActiveWorkbook
WS_Count=wbA.Worksheets.Count
strPath=wbA.Path
strTime=格式(现在(),“yyyymmdd\ \u hhmm”)
'获取活动工作簿文件夹(如果已保存)
strPath=wbA.Path
如果strPath=“”,则
strPath=Application.DefaultFilePath
如果结束
strPath=strPath&“\”
'开始循环。
对于I=1到WS\u计数
'替换图纸名称中的空格和句点
strName=Replace(wbA.Worksheets(I).Name,“,”)
strName=Replace(strName,“.”,“”)
'为savng文件创建默认名称
strFile=strName&“&&strTime&.pdf”
myFile=strPath和strFile
调试。打印我的文件
'如果选择了文件夹,则导出为PDF
如果myFile为“False”,则
ActiveSheet.ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
'带有文件信息的确认消息
MsgBox“已创建PDF文件:”_
&vbCrLf_
&我的文件
如果结束
接下来我
端接头

如果您需要任何其他详细信息,请告诉我。在将工作表打印成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
”开始循环。
对于wbA.Sheets中的每个wsA
wsA,激活
'替换图纸名称中的空格和句点
strName=Replace(wsA.Name,“,”)
strName=Replace(strName,“.”,“”)
'为savng文件创建默认名称
strFile=strName&“&&strTime&.pdf”
myFile=strPath和strFile
调试。打印我的文件
'如果选择了文件夹,则导出为PDF
如果myFile为“False”,则
ActiveSheet.ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
'带有文件信息的确认消息
MsgBox“已创建PDF文件:”_
&vbCrLf_
&我的文件
如果结束
下一个

您应该先激活每张图纸,然后再将其导出为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
选项显式
子工作表loop()
将wsA设置为工作表
将wbA设置为工作簿
作为字符串的Dim strTime
将strName设置为字符串
将strPath设置为字符串
作为字符串的Dim strFile
将strPathFile设置为字符串
Dim myFile作为变量
Dim WS_计数为整数
作为整数的Dim I
'将WS_Count设置为活动工作簿中的工作表数。
设置wbA=ActiveWorkbook
WS_Count=wbA.Worksheets.Count
strPath=wbA.Path
strTime=格式(现在(),“yyyymmdd\ \u hhmm”)
'获取活动工作簿文件夹(如果已保存)
strPath=wbA.Path
如果strPath=“”,则
strPath=Application.DefaultFilePath
如果结束
strPath=strPath&“\”
'开始循环。
对于wbA.工作表中的每个wsA
wsA,激活
'替换图纸名称中的空格和句点
strName=Replace(wsA.Name,“,”)
strName=Replace(strName,“.”,“”)
'为savng文件创建默认名称
strFile=strName&“&&strTime&.pdf”
myFile=strPath和strFile
调试。打印我的文件
'如果选择了文件夹,则导出为PDF
如果myFile为“False”,则
ActiveSheet.ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
'带有文件信息的确认消息
MsgBox“已创建PDF文件:”_
&vbCrLf_
&我的文件
如果结束
下一个wsA
端接头

销售助理的信息来自哪里?