Using Excel VBA With CATIA V6

Print Friendly, PDF & Email

Using Excel VBA With CATIA V6

When writing VBA applications that needs to communicate with another applications, we have to create the application objects for both programs. In this case were writing the application in Excel’s VBA editor, so getting the Excel application is easy.

GetObject CreateObject

Getting the CATIA application is a little harder, GetObject is used to attach to a running instance of an automation server. There are a few different ways to call GetObject, but the syntax that is recommended for the CATIA applications is as follows.

Set ioCATIA = GetObject(, "CATIA.Application")

This will only work if the CATIA application is running, if it isn’t we will get an error. In this case the CreateObject is used to start a new instance of an Automation server. There are some challenges with this especially when an application requests login or other information prior to that application fully starting.

Set ioCATIA = CreateObject("CATIA.Application")

Since the application may not be started we have to wrap the GetObject within an On Error Resume Next block and then test for an error. If an error is raised then we can try to CreateObject to launch an instance of CATIA.

	Dim ioCATIA As Variant

    On Error Resume Next
        Set ioCATIA = GetObject(, "CATIA.Application")
        If Err.Number <> 0 Then '0 means no error
            Set ioCATIA = CreateObject("CATIA.Application")
            Err.Clear
        End If
    On Error GoTo 0

Dynamic Library Link References

The next issue we have is that when working within the VBA editor for a specific application all of the API references are pre-loaded, this is not the case when working in Excels VBA editor and then wanting to work with CATIA’s API’s. So we have to manually link to the DLL references for the other application, fortunately this is somewhat simple (said with tongue in cheek).

To add a new reference, select Tools -> References… from the top dropdown menu.

Tools -> References

Here is a list of references that I would add, its highly recommended to only add the necessary references required. However some references are prerequisites to others and it can be challenging to pair it down to only the required ones.

References

Once the references are added you can automate both applications as required. The nice thing about writing the automations in Excel is that it becomes portable, as long as the machine running the application has CATIA installed.

In the sample below we can clearly we have both Application objects and we can now automate both applications.

Sub CATMain()
  
    Dim ioExcel As Application
    Set ioExcel = Excel.Application

	Dim ioCATIA As Variant

    On Error Resume Next
        Set ioCATIA = GetObject(, "CATIA.Application")
        If Err.Number <> 0 Then '0 means no error
            Set ioCATIA = CreateObject("CATIA.Application")
            Err.Clear
        End If
    On Error GoTo 0
  
    Dim ioActiveSheet As Variant
    Set ioActiveSheet = ioExcel.Sheets.Item(1)
    ioActiveSheet.Select
  
End Sub