如何解决CATIA宏中的运行时错误438?

时间:2019-02-11 21:54:43

标签: excel vba catia

我正在使用VBA在CATIA v5中编写一个宏。该程序假定从几何集中获取点并将其传输到excel文件中。我已经成功打开了excel文档,创建了一个标题,但是随后我收到“运行时错误'438':对象不支持此属性或方法。       我尝试过四处搜索,似乎该部分代码正在尝试与其域外的内容进行交互,但我不知道如何进行。以下是我的代码示例。左侧包含“ ***”的行是调试器中指出的行。

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

1 个答案:

答案 0 :(得分:0)

您的第一个问题是,在CATIA VBA的任何将数组作为参数传递的方法中,都必须在声明为variant的对象上调用(显式或默认)。 所以你应该看起来像这样:

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