更新工作表(“打印”)并导出为单个pdf

时间:2019-04-05 17:19:32

标签: excel vba

我建立了一个更新Sheet(“ print”)的循环。

每次工作表更新时,我都必须将其打印/导出为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

1 个答案:

答案 0 :(得分:0)

将同一张纸打印为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