VBA - 将单张纸打印为单独和组合的pdf

时间:2017-03-23 20:24:05

标签: vba printing

Dim s As String
Dim i As Long
Dim rng As Range
Dim ws() As Variant 'dynamic array


If LCase(Sheet76.Range("H7")) = "x" Then
    ReDim ws(1 To 7) 'declare size

    'Select file save location
    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
       .Show

       If .SelectedItems.Count > 0 Then
           strPath = .SelectedItems(1)
       End If
    End With

'to print individually
    For i = 1 To 7
        s = Sheet76.Cells(8 + i, 7).Value
        ws(i) = s
        Sheets(s).Select
        strPath = strPath & "\"
        strFileName = strPath & s & ".pdf"

        ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=strFileName, _
        IgnorePrintAreas:=False
    Next

end if

我有两个问题。上面的代码用于打印每个工作表的单独pdf。我能够打印7个中的4个,然后它有一个错误运行时消息。我究竟做错了什么?

另外,为什么这不能将多个工作表打印成单个pdf?

  sheets(ws).select  
ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=strFileName, _
        IgnorePrintAreas:=False`

编辑:我修复了运行时错误; i = 5的字符串值没有与工作表名称相关联。我仍然坚持如何将选定的工作表打印成一个合并的pdf。

1 个答案:

答案 0 :(得分:0)

    dim ws() as string        
    For i = 1 To 7
            s = Sheet76.Cells(8 + i, 7).Value
        ws(i) = s
    Next

    Sheets(ws).Select
    strFileName = strPath & "Combined.pdf"
    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strFileNamering variable., _
    IgnorePrintAreas:=False

将工作表打印为单个组合pdf的解决方案。我通过将ws()数组声明为st

来实现它