2

I have a problem with organizing my CATIA macros and I can not find a suitable solution for it.

At the moment I have a large project that grew from smaller one in time. It is 10k lines big and it is written in one module. I would like to make it more readable and easier to modify / navigate by creating multiple commented modules. I have my "Main" module and GUI that would call other modules. Problem is that CATIA makes every module I create accessible to use as a macro. I do not want that because I want user to make a shortcut for my Main module. I do not need to hide my code but it would be very hard to explain new user which macro he/she would need to use if the got 20 macros when importing one project.

I tried with "option private module" and even using password but I can not reduce number of available macros in that list. Only solution I can think of (and it is crazy) is to put every procedure/function in separate class.

Example of tree structure in VBA and CATIA macro interface

any experience or thought about this problem is welcome.

Thanks in advance! :)

Tomislav Horvat
  • 163
  • 1
  • 8

1 Answers1

2

There is absolutely nothing whatsoever that is crazy about refactoring your procedural code into class modules. In fact, I would even go as far as calling it a best practice.

Your macros can all look like this:

Public Sub DoSomething()
    With New MyAwesomeMacro
        .Execute
    End With
End Sub

Public Sub DoAnotherThing()
    With New SomeOtherMacro
        .Execute
    End With
End Sub

'...

So you have a MyAwesomeMacro class module with an Execute procedure (which can take arguments as needed):

Option Explicit

Public Sub Execute()
    'the old macro code here...
End Sub
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • Thank you Mathieu, I have been testing this yesterday and today, and this works better than I hoped for. I have just one question for you. "MyAwesomeMacro" is created after "With New" command and it is freed with "End With"? What is the scope of its existence? If I would like to preserve some of its value I would need to pass it to some other variable/object? – Tomislav Horvat Mar 06 '19 at 07:47
  • 1
    Yes, exactly; you can have `Debug.Print` statements in the `Initialize` and `Terminate` handlers to confirm this. And you can declare as local variable of that type just like any `Worksheet` or `Collection` object, and pass them around too. I just didn't see a need for that in a "create-and-run" scenario. – Mathieu Guindon Mar 06 '19 at 12:24