首页 > 解决方案 > Uptade Sheet.("print") 并导出为单个 pdf

问题描述

我建立了一个循环来更新工作表(“打印”)。

每次更新此表时,我都必须将其打印/导出为 PDF 文件。

是否可以将同一张纸多次打印/导出为单个 PDF?

'Loop
For i = 4 To lastrow
    If Planilha4.Range("B" & i).Value = Empty Then GoTo continua
        If Planilha4.Range("J" & i).Value = Range("cart_ref_tipo_cartaz").Value Then
            Range("imp_linha").Value = i - 3
            **'I'm currently printing it to Fine Print**
            PlanilhaA4.PrintOut Copies:=Range("imp_copias").Value, IgnorePrintAreas:=False           
        End If
    End If
continua: 
Next i

标签: excelvba

解决方案


在同一个 PDF 上多次打印到 PDF 的一种方法是复制该工作表并打印/保存它们。下面的代码在 Excel 2016/365 中测试。如果您的 excel 创建了一个带有 3 张纸的新 WB,请删除所有 3 张,而不是像我一样只删除一张。

Sub printSheet()
    Set wb = ThisWorkbook
    'The next line creates a new Workbook
    Set newWB = Workbooks.Add
    'The next line creates a copy of Sheet "Testing" on the new Workbook
    wb.Sheets("Testing").Copy newWB.Sheets(newWB.Sheets.Count)
    'Delete the empty Default Sheet1 from the new WB
    Application.DisplayAlerts = False
    newWB.Sheets("Sheet1").Delete
    Application.DisplayAlerts = True
    'Create two Copies of Testing to print three.
    newWB.Sheets("Testing").Copy newWB.Sheets(newWB.Sheets.Count)
    newWB.Sheets("Testing").Copy newWB.Sheets(newWB.Sheets.Count)
    'Save to PDF
    'Unfurtonately, you have to use ActiveSheet to print to PDF, so we have to use Activate and Select to print both sheets.
    newWB.Activate
    newWB.Sheets(Array(newWB.Worksheets(1).Name, newWB.Worksheets(2).Name, newWB.Worksheets(3).Name)).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\username\Documents\filename.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=True
    'close new WB
    newWB.Close False
End Sub

推荐阅读