如何在Excel VBA中打印速度更快?

Excel的打印function(使用VBA)非常慢。 我希望有人有办法加快打印(不使用Excel 4macros技巧)。 以下是我现在的做法:

Application.ScreenUpdating = False With ActiveSheet.PageSetup -various setup statements which I've already minimized- End With ActiveSheet.PrintOut Application.ScreenUpdating = True 

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

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

此外,每个PageSetup属性获取的属性非常快,而只有属性集非常慢。 因此,你应该testing新的属性设置,以确保它不是已经与现有的属性值相同,以防止不必要(和昂贵)的调用。

考虑到这一切,你应该可以使用类似下面的代码:

 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 4macros迁移到VBA 。

  2. 对于Excel 2007及以下版本,另一个有趣的技巧是暂时将打印机驱动程序分配给“Microsoft XPS Document Writer”,然后将其设置回来。 打印速度可以提高3倍。 请参阅: 缓慢的Excel PageSetup方法 。

希望这可以帮助…

在推进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 

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

 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.Dialogsfunction,此代码不提供最干净的可能的解决scheme。 然而,它完成了工作。 🙂

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