How to add and remove references using CATVBA

Let’s say you are exporting data from CATIA to Excel or CATIA to Word, but different coworkers have different versions of Excel (Excel 2010, Excel 2003, Excel, 2013, etc.). If your users have an earlier version of MS Excel than the person that last saved the application, then the MS Excel reference will become broken. If this is the scenario then you should develop your application using early binding, but then convert it to late binding and remove the MS Excel reference entirely before it is deployed. That way the reference will not be an issue. You must, of course, ensure that the code which automates MS Excel is compatible for all versions in use by the end users. I will get into early versus late binding more later but for now let’s pretend you already have an existing code using late binding.

dd reference in vba

A Reference in a VBA Project is a “pointer” to a type library or DLL file that defines various objects and entities and the properties and methods of those entities. You may need to check the reference libraries in CATIA programmatically to prevent any errors from happening and so your users don’t ever have to go into the VB editor. Or if there is a problem the user at least knows what to do to fix it.

The codes below show you how to:

  • Check what version of Excel the user has
  • Check what references are selected
  • Remove a reference
  • Add a reference from a file location
  • Use a For Each Next loop

If there is a reference in the VB Project that refers to a library that VBA cannot find on the local machine, VBA marks that reference as MISSING in the References dialog box.

If the reference is already missing, you will probably not be able to remove it via VBA, but you can trigger a message box warning the user that he needs to manually remove the missing reference before the program can be run successfully.

 

Overall, the code could probably be better but I wanted to give a few examples on a couple of different things all at once and try to get you thinking. Have you ever had to do anything like this or had any trouble with references? Please let me know!

 

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *