3

Excel 的打印功能(使用 VBA)非常慢。我希望有人能加快打印速度(不使用 Excel 4 宏技巧)。这是我现在的做法:

Application.ScreenUpdating = False

With ActiveSheet.PageSetup

  -various setup statements which I've already minimized-

End With   
ActiveSheet.PrintOut

Application.ScreenUpdating = True
4

3 回答 3

9

是的,PageSetup 属性在设置时非常慢。

您已经设置了Application.ScreenUpdating = False,这很好,但在这种情况下,同样(或更重要)的一步是设置Application.Calculation = xlCalculationManual。(最好保存这些设置,然后在最后将它们恢复为原始设置。)

此外,每个 PageSetup 属性的属性获取都非常快,而只有属性集如此缓慢。因此,您应该测试新的属性设置以确保它与现有的属性值不同,以防止不必要的(和昂贵的)调用。

考虑到所有这些,您应该能够使用如下所示的代码:

Dim origScreenUpdating As Boolean
origScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False

Dim origCalcMode As xlCalculation
origCalcMode =  Application.Calculation
Application.Calculation = xlCalculationManual

With ActiveSheet.PageSetup
    If .PrintHeadings <> False Then .PrintHeadings = False
    If .PrintGridlines <> False Then .PrintGridlines = False
    If .PrintComments <> xlPrintNoComments Then .PrintComments = xlPrintNoComments
    ' Etc...
End With

Application.ScreenUpdating = origScreenUpdating
Application.Calculation = origCalcMode

编辑:几个更新:

  1. 对于 Excel 2010 及更高版本,您可以使用“Application.PrintCommunication”属性,而对于 Excel 2007 及更低版本,您可以使用“ExecuteExcel4Macro”。有关更多详细信息,请参阅将Excel 4 宏迁移到 VBA

  2. 对于 Excel 2007 及更低版本,另一个有趣的技巧是将打印机驱动程序临时分配给“Microsoft XPS 文档编写器”,然后将其重新设置。打印速度可提高 3 倍。请参阅:慢速 Excel PageSetup 方法

希望这可以帮助...

于 2008-10-23T19:38:03.043 回答
2

在推进 Michael 的帖子并回答 @rhc 的问题时,如果需要将页面设置自定义项从单个工作表复制到工作簿中的多个工作表,以下代码也可以帮助您:

Public Sub CopyPageSetupToAll(ByRef SourceSheet As Worksheet)
    ' Raise error if invalid source sheet is passed to procedure
    '
    If (SourceSheet Is Nothing) Then
        Err.Raise _
            Number:=vbErrorObjectVariableNotSet, _
            Source:="CopyPageSetupToAll", _
            Description:="Unable to copy Page Setup settings: " _
                 & "invalid reference to source sheet."
        Exit Sub
    End If

    SourceSheet.Activate

    With SourceSheet.PageSetup
        ' ...
        ' place PageSetup customizations here
        ' ...
    End With

    SourceSheet.Parent.Worksheets.Select
    Application.SendKeys "{ENTER}", True
    Application.Dialogs(xlDialogPageSetup).Show
End Sub

或者,您也可以修改该过程以创建临时工作表来托管您的页面设置更改,然后将这些更改传播到工作簿中的其他工作表:

Public Sub CopyPageSetupToAll(ByRef SourceBook As Workbook)
    Dim tempSheet As Worksheet

    ' Raise error if invalid workbook is passed to procedure
    '
    If (SourceBook Is Nothing) Then
        Err.Raise _
            Number:=vbErrorObjectVariableNotSet, _
            Source:="CopyPageSetupToAll", _
            Description:="Unable to copy Page Setup settings: " _
                 & "invalid reference to source workbook."
        Exit Sub
    End If

    Set tempSheet = SourceBook.Worksheets.Add

    tempSheet.Activate

    With tempSheet.PageSetup
        ' ...
        ' place PageSetup customizations here
        ' ...
    End With

    SourceBook.Worksheets.Select
    Application.SendKeys "{ENTER}", True
    Application.Dialogs(xlDialogPageSetup).Show
    tempSheet.Delete

    Set tempSheet = Nothing
End Sub

由于SendKeys()函数和Application.Dialogs功能的使用,此代码不能提供最简洁的解决方案。但是,它可以完成工作。:)

于 2009-05-14T23:33:27.207 回答
0

如果您想为工作簿中的每个选项卡设置基本相同的页面设置,您可以通过设置一个工作表然后将该工作表的设置以某种方式复制到其他工作表来加快速度吗?这可能吗?

于 2008-11-12T16:52:10.490 回答