【发布时间】:2015-05-23 20:47:33
【问题描述】:
我目前有一个将整个 Excel 工作表导出为 PDF 的 VBScript。我想知道是否可以将脚本设置为仅导出选定区域。我知道这可以手动完成,但我的任务是自动化这个过程。如果实现了,它将使整个报告过程变得容易得多。我目前使用的 VBScript 是:
Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Set ws = ActiveSheet
'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
& "_" _
& Format(Now(), "yyyymmdd\_hhmm") _
& ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "PDF version of report has been created."
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
我已经尝试将“Set ws = ActiveSheet”替换为“Selection”,但没有产生任何结果。此外,作为侧面笔记,当将选择发送到PDF时,它们出现在单独的工作表上。可以在一张纸上放置它们?
提前感谢您为我提供的任何帮助。
【问题讨论】: