声明为应用程序typesvariables的子参数

我有这个子例程打开/closures一些Application的属性。

 Public Sub OPTIMIZE_VBA(ByVal isOn As Boolean) Dim bHolder As Boolean bHolder = Not isOn On Error Resume Next With Application .DisplayAlerts = bHolder .ScreenUpdating = bHolder .EnableEvents = bHolder .Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic) .Calculate If .VERSION > 12 Then .PrintCommunication = bHolder End With On Error GoTo 0 End Sub 

说有一个不同的应用程序实例,我希望打开/closures相同的属性,我怎么能够修改这个代码接受一个参数/参数作为一个不同的应用程序? 我期待着像

 Public Sub OPTIMIZE_VBA(ByVal isOn As Boolean, Optional ByVal ExApp As Excel.Application = thisApplication) ' rest of the code End Sub 

有了这个,我可以这样称呼它;

 Sub Create_New_Excel_and_Disable_Properties() '<~ Declare and prepare the new application Dim NewExcel As Excel.Application Set NewExcel = New Excel.Application '<~ declare and set the workbook variable Dim ExcelWbk As Excel.Workbook Set ExcelWbk = NewExcel.Workbooks.Open("folder\template.xlsx") '<~ call the sub that disables the Application.properties OPTIMIZE_VBA False, NewExcel End Sub 

 Public Sub OPTIMIZE_VBA(ByVal isOn As Boolean, Optional ByVal ExApp As Excel.Application) If IsEmpty(ExApp) Or ExApp Is Nothing Then Set ExApp = Application End If ' rest of the code End Sub 

单程:

 Public Sub OPTIMIZE_VBA(ByVal isOn As Boolean, Optional ByVal ExApp As Excel.Application = Nothing) If ExApp Is Nothing Then Set ExApp = Application ' rest of the code End Sub