2008-10-23 114 views
3

Excel(使用VBA)的打印功能非常慢。我希望有人有办法加快打印速度(不使用Excel 4 Macro技巧)。以下是我现在如何做的:如何在Excel VBA中更快打印?

Application.ScreenUpdating = False 

With ActiveSheet.PageSetup 

    -various setup statements which I've already minimized- 

End With 
ActiveSheet.PrintOut 

Application.ScreenUpdating = True 

回答

8

是的,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'。有关更多详细信息,请参阅Migrating Excel 4 Macros to VBA

  2. 对于Excel 2007及更低版本,另一个有趣的技巧是暂时将打印机驱动程序分配给“Microsoft XPS Document Writer”,然后将其重新设置。打印速度可提高3倍。参见:Slow Excel PageSetup Methods

希望这有助于...

如果你想有basicly在工作簿,你可以通过设置一个workshet然后以某种方式复制该工作表的设置,加快速度每一个标签相同的页面设置
+0

我会测试一下,并让你知道它是如何为我工作的。 – 2008-10-23 19:45:18

0

到其他工作表?这可能吗?

2

在促进迈克尔的职位,并回答@ 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()与0函数功能,此代码不提供最干净的可能解决方案。但是,它完成了工作。 :)