Excel VBA将多个工作表导出为PDF

时间:2016-04-13 11:51:35

标签: excel vba excel-vba pdf excel-2010

我一直在尝试将多个工作表上的命名范围导出到单个pdf。最初,它似乎工作正常,然后我注意到在将各种工作表加载到数组中时所选范围会发生变化。

我的理解是使用Selection.ExportAsFixedFormat这只会导出所选的单元格。因此,我有一个宏来循环遍历所需的工作表,选择需要的各种范围,然后为所有工作表创建一个数组,以允许导出到单个pdf。

Dim wb As Workbook
Dim srcSht As Worksheet
Dim toPrnt As String

Set wb = ThisWorkbook
Set srcSht = wb.Sheets("print_array")

Dim myArr1() As Variant
    myArr1 = srcSht.Range("myPrintArray")

Dim i As Long
Dim j As String
    For i = LBound(myArr1, 1) To UBound(myArr1, 1)
            j = myArr1(i, 1)

    wb.Sheets(j).Activate
        wb.ActiveSheet.Range("CCB_" & Left(j, 5) & "_Print").Select

    Next i

wb.Sheets(Array("CAT01 - Request for Resource", _
                            "CAT02 - Resource Allocation", _
                            "CAT03 - Product Data Sources", _
                            "CAT04 - Target & Control Cells", _
                            "CAT05 - Matching And Deduping", _
                            "CAT06 - Exclusions", _
                            "CAT07 - Data from other teams", _
                            "CAT08 - Outputs", _
                            "CAT09 - Special Instructions", _
                            "CAT10 - Brief Meeting Sign Off" _
                            )).Select

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF_FileAndPath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True

当单步执行代码时,所有内容都会计划到创建工作表数组,此时所选范围会发生变化。

我也尝试过使用PageSetup,但结果是一样的。

With ActiveSheet.PageSetup
    .Orientation = xlLandscape
    .PaperSize = xlPaperA4
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .PrintArea = Range("CCB_" & Left(j, 5) & "_Print").Address
End With

在本次论坛上回顾了几篇类似的帖子后,我仍然感到茫然。

任何人都可以了解创建阵列时所选范围发生变化的原因,还是有其他可能有帮助的建议?

非常感谢

2 个答案:

答案 0 :(得分:0)

我设法通过将所选范围复制到临时文件然后从那里导出来解决我的问题。完整的解决方案看起来像这样......

Dim wb As Workbook
Dim srcSht As Worksheet
Dim tempFile As String

Set wb = ThisWorkbook
Set srcSht = wb.Sheets("print_array")

Dim myArr1() As Variant
    myArr1 = srcSht.Range("myPrintArray")

Dim i As Long
Dim j As String
    For i = LBound(myArr1, 1) To UBound(myArr1, 1)
            j = myArr1(i, 1)

    wb.Sheets(j).Activate

    With ActiveSheet.PageSetup
        .Orientation = xlLandscape
        .PaperSize = xlPaperA4
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PrintArea = Range("CCB_" & Left(j, 5) & "_Print").Address
    End With

        Next i

    wb.Sheets(Array("CAT01 - Request for Resource", _
                                "CAT02 - Resource Allocation", _
                                "CAT03 - Product Data Sources", _
                                "CAT04 - Target & Control Cells", _
                                "CAT05 - Matching And Deduping", _
                                "CAT06 - Exclusions", _
                                "CAT07 - Data from other teams", _
                                "CAT08 - Outputs", _
                                "CAT09 - Special Instructions", _
                                "CAT10 - Brief Meeting Sign Off" _
                                )).copy

                With ActiveWorkbook
                    .Save

                    tempFile = .FullName

                    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF_FileAndPath, _
                        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                        :=False, OpenAfterPublish:=True

                    .Close

                End With

                Kill tempFile

   End If

我希望将来可以帮助某人。

感谢。

答案 1 :(得分:0)

我遇到了同样的问题。我遇到了另一种更简单的解决方案。不需要临时文件。

在您选择阵列后,而不是选择 .ExportAsFixedFormat,请使用 ActiveSheet .ExportAsFixedFormat。由于您的工作表在初始.select中处于活动状态,因此ActiveSheet会选择要导出的所有所需工作表。

您的代码应如下所示:

{{1}}