0

CATIAV5 manages vba projects in specific files with extension .catvba

These projects are structured and behave like other VBAs: they have Modules, Forms and Classes. You can manually import export this VBA components, as you can do for example in Excel. But it seems that in CATIA you can't automate this operation, while this is possible in Excel using the "Microsoft Visual Basic For Applications Extensibility" module (as you can see here https://www.rondebruin.nl/win/s9/win002.htm).

I would like to achieve something similar in CATIAV5.

In other words, in order to have my .catvbas under version control I would like to:

  • automate import/export of modules/classes/forms and "assemble" and "disassemble" them in a .catvba file programmatically.

By now I managed to add the Extensibility Library on a sample catvba project, and access the current project components for import or export.

'These references were added in the project:
'Microsoft APC  Object Library
'Microsoft Visual Basic for Applications Extensibility
Sub main()

    Dim oAPC As New Apc
    Dim oVBE As VBE

    Set oVBE = oAPC.VBE
    Dim p As Project

    Set p = oVBE.VBProjects.add(vbext_pt_HostProject)
    'the instruction above throws error 440

    Set p = oVBE.VBProjects.Open("C:\path\to\another\existing\project.catvba")
    'the instruction above throws error 440, too

    For Each comp In oAPC.VBE.ActiveVB.Project.VBComponent
    Debug.Print comp.Name
    '...export comp
    Next

BUT STILL I'cant create a new catvba project, or open an existing one (error 440, see above). If I only could be able to open another project I could use it as an empty template to "fill"..

Federico Destefanis
  • 968
  • 1
  • 16
  • 27

1 Answers1

0

After painful trial and error I came up to a working prototype.

To build a new empty project and populate it with your exported modules/form/classes, create a module with this code:

'remember to add "Microsoft APC" into the references
Sub main()

    Dim apc_global As New ApcGlobal
    Dim apc_framework As New Apc

    Dim apc_project As project
    Dim vb_project As VBProject

    Dim rootStorage As Storage
    Dim innerStorage As Storage

    Set apc_project = apc_framework.Projects.add(axProjectNormal, "hello")
    Set vb_project = apc_project.VBProject

    Set rootStorage = apc_global.CreateStorage("C:\hello.catvba", axAccessReadWrite)
    'you should create an innerstorage called "apc". I got crazy to find this out
    Set innerStorage = rootStorage.CreateStorage("apc", axAccessReadWrite)

    'here you should add the project references
    'vb_project.References.addFromGUID.... 

    'then you can import your previously exported files
    vb_project.VBComponents.Import("path_of_your_module/class/form files")

    Call apc_project.SaveAs(innerStorage)

    apc_project.Close

End Sub

So basically you ave to

  1. Create an APCProject with two "storages", a root storage and a child storage called "apc".
  2. Then you can create your vb project and import the components you previously have exported as .bas .cls .frm files etc...

This inner storage is where you have to save your apc project

And there you have a brand new catvba project built programmatically! Hope this will help

Federico Destefanis
  • 968
  • 1
  • 16
  • 27