Excel应用程序二次实例的计算模式

时间:2017-06-01 09:18:43

标签: excel vba excel-vba

我注意到在使用第二个Excel实例时,尝试访问计算模式属性会导致运行时错误。有人见过这个吗?单步执行以下代码将复制(我已经评论了底部附近的错误行)

Sub test1()

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With

Debug.Print "This Application:"
Debug.Print vbTab & "ScreenUpdating: " & Application.ScreenUpdating
Debug.Print vbTab & "Calculation Mode: " & Application.Calculation
Debug.Print vbTab & "Events: " & Application.EnableEvents
Debug.Print "--------------------------------------------------------"

With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

Debug.Print "This Application (After Change):"
Debug.Print vbTab & "ScreenUpdating: " & Application.ScreenUpdating
Debug.Print vbTab & "Calculation Mode: " & Application.Calculation
Debug.Print vbTab & "Events: " & Application.EnableEvents
Debug.Print "--------------------------------------------------------"

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application

Debug.Print "New Application:"
Debug.Print vbTab & "ScreenUpdating: " & xlApp.ScreenUpdating
Debug.Print vbTab & "Calculation Mode: " & xlApp.Calculation    '// Run-time error 13: Type mismatch
Debug.Print vbTab & "Events: " & xlApp.EnableEvents
Debug.Print "--------------------------------------------------------"

xlApp.Quit

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With

End Sub

1 个答案:

答案 0 :(得分:2)

你添加/创建了一个新的excel实例,但它还没有附带工作簿。 excel的新实例还没有可以使用的工作簿。无论是否可见它都会引发错误。您可以添加像;

这样的工作簿
xlApp.Workbooks.Add

HTH。