【问题标题】:VBScript to export selected portion of Excel sheet to a PDF, on one pageVBScript 将 Excel 工作表的选定部分导出为 PDF,在一页上
【发布时间】: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时,它们出现在单独的工作表上。可以在一张纸上放置它们?

提前感谢您为我提供的任何帮助。

【问题讨论】:

    标签: vba excel pdf vbscript


    【解决方案1】:

    尝试设置您要输出为 PDF 的 Print Area

    【讨论】:

    • 感谢您的回答。唯一的问题是每次都会是不同的区域,因此每次都必须更改代码并不理想。您知道是否可以在不更改代码的情况下导出我的手动选择?
    • 您可以根据要导出的内容动态设置这些范围。 Dim exportRange as RangeSet exportRange = ActiveSheet.Selection 类型的语句。
    猜你喜欢
    • 2014-01-12
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2021-10-31
    • 2012-10-16
    相关资源
    最近更新 更多