Catia VBA-自动化错误获取对象 [英] Catia VBA - Automation Error Get Object
问题描述
当Catia尝试在选定的Excel工作表中写入值时,出现自动化错误。这有点令人困惑,因为在第一次尝试代码时没有错误,并且值在Excel工作表中。
I am getting an Automation error, when Catia is trying to write values in a selected Excel sheet. It's a bit confusing because on the first try of the code there was no error and the values were in the Excel sheet.
我没有更改代码,但是在第二次尝试中我得到了:
I didn't change the code, but on the second try I get:
Run-time error '-2147417846 (8001010a)': Automation error
"The Message filter indicated that the application is busy."
在线: Set MyXL = GetObject(FPath)
Sub CATMain()
FPath = CATIA.FileSelectionBox("Select the Excel file you wish to put the value in", "*.xlsx", CatFileSelectionModeOpen)
If FPath = "" Then
Exit Sub
End If
Set xlApp = CreateObject("Excel.Application")
Set MyXL = GetObject(, "Excel.Application")
Set MyXL = GetObject(FPath)
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
Dim oSelection As Selection
Set oSelection = CATIA.ActiveDocument.Selection
Dim oProduct As AnyObject
On Error Resume Next
Set oProduct = oSelection.FindObject("CATIAProduct")
If (Err.Number <> 0) Then
MsgBox "No selected product"
Else
On Error GoTo 0
Dim oInertia As AnyObject
Set oInertia = oProduct.GetTechnologicalObject("Inertia")
Dim dMass As Double
dMass = oInertia.Mass
Dim dDen As Double
dDen = oInertia.Density
MsgBox oProduct.Name & ": Masse = " & CStr(dMass) & " KG" & ": Dichte = " & (CStr(dDen) / 1000) & " "
MyXL.Application.Cells(1, 1).Value = "Masse"
MyXL.Application.Cells(2, 1).Value = dMass
MyXL.Application.Cells(1, 2).Value = "Dichte"
MyXL.Application.Cells(2, 2).Value = "dDen"
MsgBox "Werte wurden in Excel eingetragen"
End If
End Sub
推荐答案
您似乎没有设置 Option Explicit
-将其放在第一行,它将帮助您避免错误。 (有了它,编译器将强制您声明所有变量。这也意味着,当您将其放入时,除非声明所有变量,否则代码将无法工作。)
It appears you did not set Option Explicit
- put it on the first line and it will help you avoid errors. (With it, the compiler will force you to declare all your variables. This will also mean that when you put it in, your code will not work unless you declare all variables.)
第一个问题:
Set xlApp = CreateObject("Excel.Application")
Set MyXL = GetObject(, "Excel.Application")
您首先使用 CreateObject
创建一个新的Excel实例,并将对它的引用存储在 xlApp
中(随后进行了操作)不使用)。然后,尝试使用 GetObject
获取对现有 Excel实例的引用,并将其引用存储在 MyXL $ c中$ c>。这只能可靠地起作用,因为您首先创建了一个新实例。否则,您不能保证总是有一个Excel实例。
You first create a new instance of Excel with CreateObject
and store a reference to it in xlApp
(which you subsequently do not use). Then you try to get a reference to an existing Excel instance with GetObject
and store its reference in MyXL
. This only works reliably because you first create a new instance. Otherwise you could not guarantee that there always is an Excel instance available.
一个相关的问题是,您不释放/关闭这些实例。如果创建一个Excel实例,则在使用完它后,需要使用 xlApp.Quit
将其关闭,否则它将持续存在。
但是对于使用 GetObject
接管的实例要小心-调用 MyXL.Quit
会关闭该实例,无论其他工作簿如何
A related problem is, that you don't release/close these instances. If you create an Excel instance, you need to close it with xlApp.Quit
after you're done using it, otherwise it will linger around.
Be careful though with instances you took over with GetObject
- calling MyXL.Quit
will close the instance regardless of what other workbooks are open at that time.
类似地,如果您以这种方式打开文件,则需要确保随后将其关闭。否则,您将遇到遇到的问题:编写受保护的文件。
Similarly, if you open a file this way, you need to make sure to close it afterwards. Otherwise you'll run into the problem you experience: Write protected files.
因此,要修复您的问题:关闭所有打开Excel实例(最好通过任务管理器完成,因为其中有些可能不可见)。然后调整您的代码,使其仅使用对 Excel.Application
的引用。最后,请确保在保存工作簿后。关闭
工作簿,并
So, to mend your problem: Close all open instances of Excel (best done via Task Manager, as some of them might be invisible). Then adjust your code to only use one reference to an Excel.Application
. And finally make sure to .Close
the workbook after you've saved it and .Quit
your Excel instance. This should hopefully prevent the error from reappearing.
'Dim xlApp As Excel.Application ' early-bound declaration
'Set xlApp = New Excel.Application ' early-bound assignment
Dim xlApp As Object ' late-bound declaration
Set xlApp = CreateObject("Excel.Application") ' late-bound assignment
'Dim wb As Workbook ' early-bound declaration
Dim wb as Object
Set wb = xlApp.Workbooks.Open(FPath)
' stuff you want to do with the workbook
wb.Close SaveChanges:=True
xlApp.Quit
如果可以在Catia VBA项目中添加对Excel对象模型的引用(对此不确定),则可以注释掉后期绑定的行,而改用早期绑定的行。这样,您就可以为Excel对象获得非常有用的IntelliSense。这使得编写代码变得如此容易。
If you can add a reference to the Excel object model in you Catia VBA project (not sure about that), you can comment out the late-bound lines and use the early-bound lines instead. That way you gain the very useful IntelliSense for the Excel objects. Which makes it so much easier to code.
这篇关于Catia VBA-自动化错误获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!