Search code examples
excelvbacatia

How do I resolve Run-time Error 438 inside a CATIA macro?


I am writing a macro in CATIA v5 using VBA. The program is suppose to take points from a geometric set and transfer them into an excel file. I have successfully gotten the excel document open, a header created, but then I receive "Run-time error '438': Object doesn't support this property or method. I have tried searching around and it seems like the section of code is trying to interact with something outside of its domain, but I cannot figure out how. Below is a sample of my code. The line that contains "***" to the left is the line that is being pointed out in the debugger.

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

Solution

  • Your first issue is that in any method in CATIA VBA which passes an array as an argument, must be called on a object declared variant (explicitly or by default). So you it should look like this:

    Dim px as Variant
    Set px = CATIA.ActiveDocument.Selection.Item(i).Value
    Call Point.GetCoordinates(coords)
    

    The second problem is that in VBA if you use a subroutine with parentheses, you must use the Call keyword:

    Call Point.GetCoordinates (coords)
    

    Otherwise, you can skip the parentheses and the keyword:

    Point.GetCoordinates coords