如何解决 CATIA 宏中的 Run-time 错误 438?
How do I resolve Run-time Error 438 inside a CATIA macro?
我正在使用 VBA 在 CATIA v5 中编写宏。该程序假设从几何集中获取点并将它们传输到 excel 文件中。我已成功打开 excel 文档,创建 header,但随后我收到“Run-time 错误‘438’:Object 不支持此 属性 或方法。
我试过四处搜索,似乎这部分代码正试图与其领域之外的事物进行交互,但我不知道如何进行。下面是我的代码示例。左侧包含“***”的行是调试器中指出的行。
Dim xls As Object
Dim wkbks As Object
Dim wkbk As Object
Dim wksheets As Object
Dim sheet As Object
Dim fs, f, f1, fc, s
Dim coords(2) As Integer
Dim PartDoc
Sub CATMain()
CATIA.ActiveDocument.Selection.Search "CATGmoSearch.Point,all"
'Function Calls
AppStart
CATIAtoXLS
'wksheet.Application.ActiveWorkbook.SaveAs (ExcelFolder & Left(CATIA.ActiveDocument.Name,Len(CATIA.ActiveDocument.Name)-8)&".xls")
'wksheet.Application.ActiveWorkbook.Close
End Sub
Private Sub AppStart()
Err.Clear
On Error Resume Next
Set xls = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set xls = CreateObject("Excel.Application")
End If
xls.Application.Visible = True
Set wkbks = xls.Application.Workbooks
Set wkbk = wkbks.Add
Set wksheets = wkbk.Worksheets(1)
Set sheet = wkbk.Sheets(1)
sheet.Cells(1, "A") = "X-Cord"
sheet.Cells(1, "B") = "Y-Cord"
sheet.Cells(1, "C") = "Z-Cord"
End Sub
Private Sub CATIAtoXLS()
For i = 1 To CATIA.ActiveDocument.Selection.Count
Set Selection = CATIA.ActiveDocument.Selection ***
Set Element = Selection.Item(i)
'Transfer data to xls
Point.GetCoordinates (coords)
sheet.Cells(i + 1, "A") = coords(0)
sheet.Cells(i + 1, "B") = coords(1)
sheet.Cells(i + 1, "C") = coords(2)
Next i
End Sub
您的第一个问题是,在 CATIA VBA 中将数组作为参数传递的任何方法中,必须在声明为变体的对象上调用(显式或默认)。
所以你应该看起来像这样:
Dim px as Variant
Set px = CATIA.ActiveDocument.Selection.Item(i).Value
Call Point.GetCoordinates(coords)
第二个问题是在VBA中如果使用带括号的子程序,必须使用Call关键字:
Call Point.GetCoordinates (coords)
否则,您可以跳过括号和关键字:
Point.GetCoordinates coords
我正在使用 VBA 在 CATIA v5 中编写宏。该程序假设从几何集中获取点并将它们传输到 excel 文件中。我已成功打开 excel 文档,创建 header,但随后我收到“Run-time 错误‘438’:Object 不支持此 属性 或方法。 我试过四处搜索,似乎这部分代码正试图与其领域之外的事物进行交互,但我不知道如何进行。下面是我的代码示例。左侧包含“***”的行是调试器中指出的行。
Dim xls As Object
Dim wkbks As Object
Dim wkbk As Object
Dim wksheets As Object
Dim sheet As Object
Dim fs, f, f1, fc, s
Dim coords(2) As Integer
Dim PartDoc
Sub CATMain()
CATIA.ActiveDocument.Selection.Search "CATGmoSearch.Point,all"
'Function Calls
AppStart
CATIAtoXLS
'wksheet.Application.ActiveWorkbook.SaveAs (ExcelFolder & Left(CATIA.ActiveDocument.Name,Len(CATIA.ActiveDocument.Name)-8)&".xls")
'wksheet.Application.ActiveWorkbook.Close
End Sub
Private Sub AppStart()
Err.Clear
On Error Resume Next
Set xls = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set xls = CreateObject("Excel.Application")
End If
xls.Application.Visible = True
Set wkbks = xls.Application.Workbooks
Set wkbk = wkbks.Add
Set wksheets = wkbk.Worksheets(1)
Set sheet = wkbk.Sheets(1)
sheet.Cells(1, "A") = "X-Cord"
sheet.Cells(1, "B") = "Y-Cord"
sheet.Cells(1, "C") = "Z-Cord"
End Sub
Private Sub CATIAtoXLS()
For i = 1 To CATIA.ActiveDocument.Selection.Count
Set Selection = CATIA.ActiveDocument.Selection ***
Set Element = Selection.Item(i)
'Transfer data to xls
Point.GetCoordinates (coords)
sheet.Cells(i + 1, "A") = coords(0)
sheet.Cells(i + 1, "B") = coords(1)
sheet.Cells(i + 1, "C") = coords(2)
Next i
End Sub
您的第一个问题是,在 CATIA VBA 中将数组作为参数传递的任何方法中,必须在声明为变体的对象上调用(显式或默认)。 所以你应该看起来像这样:
Dim px as Variant
Set px = CATIA.ActiveDocument.Selection.Item(i).Value
Call Point.GetCoordinates(coords)
第二个问题是在VBA中如果使用带括号的子程序,必须使用Call关键字:
Call Point.GetCoordinates (coords)
否则,您可以跳过括号和关键字:
Point.GetCoordinates coords