在Excel中将多个选择打印为1张单页PDF

时间:2019-09-16 08:34:02

标签: excel vba pdf

我是Excel VBA的新手,我从https://trumpexcel.com/convert-excel-to-pdf/获得了此代码以将所选内容打印为PDF。

目前,我唯一关心的是如何将所有不连续的选择打印到1个PDF中。如果有人可以提供帮助,我将非常感谢。

例如,在下面的照片中,您可以看到,如果我从第一行选择到最后一行,那么当我将Selection打印为PDF时,它将转换为1页。但是例如,我只想打印产品1和产品3(跳过产品2),那么我将只能将它们打印到2个单独的页面中。我该如何将其分成1页。 代码:

Sub PrintSelectionToPDF()
    Dim ThisRng As Range
    Dim strfile As String
    Dim myfile As Variant

    If Selection.Count = 1 Then
        Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
    Else
        Set ThisRng = Selection
    End If

    'Prompt for save location
    strfile = "Selection" & "_" _
        & Format(Now(), "yyyymmdd_hhmmss") _
        & ".pdf"
    strfile = ThisWorkbook.Path & "\" & strfile
    myfile = Application.GetSaveAsFilename _
        (InitialFileName:=strfile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and File Name to Save as PDF")
    If myfile <> "False" Then 'save as PDF
        ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, OpenAfterPublish:=True
    Else
        MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
    End If
End Sub

Picture

0 个答案:

没有答案