2008-10-23 117 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函數功能,此代碼不提供最乾淨的可能解決方案。但是,它完成了工作。 :)