简体   繁体   English

如何在循环中将多个页面打印到一个pdf

[英]How to print multiple pages to one pdf in cycle

I am trying to create macro for printing daily planner. 我正在尝试创建用于打印日常计划的宏。 One page for each day. 每天一页。 I have created some kind of template and now, each time code run trough for cycle, it changes date and week number etc. and then print the page. 我已经创建了某种模板,现在,每次代码都会循环运行,它会更改日期和周数等,然后打印页面。 However like this is every page in different pdf file. 但是,这是每个页面在不同的pdf文件中。 Is there some way, how to add each time new page to the same pdf (same range, but different data) and then print? 有没有办法,如何将每次新页面添加到相同的pdf(相同范围,但不同的数据),然后打印?

For i = 1 To 365

ActiveSheet.PrintOut

Range("A26") = WorksheetFunction.RoundUp((i + 2) / 7, 0) & ". week" 'week number

Range("A1").Value = Range("A1").Value + 1  'change date

Range("A1").Select
If (i Mod 2 = 0) Then
    Selection.HorizontalAlignment = xlLeft 'left page
Else
    Selection.HorizontalAlignment = xlRight 'right page
End If

Next i

I have amended your macro so that it first creates a new temporary workbook in which to copy each updated worksheet. 我已修改您的宏,以便它首先创建一个新的临时工作簿,在其中复制每个更新的工作表。 Then it uses the ExportAsFixedFormat method to print the workbook to the specified PDF file (change the filename accordingly). 然后,它使用ExportAsFixedFormat方法将工作簿打印到指定的PDF文件(相应地更改文件名)。 Then it deletes the temporary workbook. 然后它删除临时工作簿。 Note that ScreenUpdating is set to False at the beginning of the code so that all this happens in the background. 请注意,ScreenUpdating在代码的开头设置为False,以便所有这些都在后台进行。 Also, it is assumed that the template sheet is the active sheet. 此外,假设模板表是活动表。

Option Explicit

Sub PrintDailyPlanner()

    Dim sourceWS As Worksheet
    Dim tempWB As Workbook
    Dim i As Long

    Application.ScreenUpdating = False

    'set the active sheet
    Set sourceWS = ActiveSheet

    'create a new temporary workbook with one worksheet
    Set tempWB = Workbooks.Add(Template:=xlWBATWorksheet)

    'copy the source worksheet each time it's updated to the newly created temporary workbook
    With sourceWS
        For i = 1 To 365

            .Range("A26") = WorksheetFunction.RoundUp((i + 2) / 7, 0) & ". week" 'week number

            .Range("A1").Value = .Range("A1").Value + 1  'change date

            If (i Mod 2 = 0) Then
                .Range("A1").HorizontalAlignment = xlLeft 'left page
            Else
                .Range("A1").HorizontalAlignment = xlRight 'right page
            End If

            .Copy after:=tempWB.Worksheets(tempWB.Worksheets.Count)

        Next i
    End With

    'delete the first worksheet from the temporary workbook
    Application.DisplayAlerts = False
    tempWB.Worksheets(1).Delete
    Application.DisplayAlerts = True

    'print the temporary workbook to PDF file (change the filename accordingly)
    tempWB.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\Domenic\Desktop\sample.pdf"

    'close the temporary workbook, without saving
    tempWB.Close SaveChanges:=False

    Application.ScreenUpdating = True

End Sub

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM