【发布时间】:2018-06-28 09:17:09
【问题描述】:
我正在尝试创建一个宏,该宏将为工作簿中的选定工作表生成单独的 PDF 文件。我在网上找到了这段代码,效果很好,它将所有选定的工作表放在一个 PDF 中。我需要将每个选定的工作表作为单独的 PDF。任何帮助将不胜感激!
我是 VBA 的初学者。再次感谢!
Sub PDFActiveSheet()
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
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
wsA.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
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
【问题讨论】:
-
执行您的代码,将所有选定的工作表放在一个 pdf 中,因为这也是我找到的代码的作用。我需要它们是单独的 pdf 的....
-
然后,一旦你完成了这个 pdf,得到一个脚本来生成单独的 pdf - 我找到了一个苹果脚本,它可以作为一种享受,但有些必须存在于“另一端”......或者你可以看看在这里,因为之前已经提出过这种类型的问题..