VBA Module Naming Best Practices

VBA Module Naming Best Practices

eric.frissell26WKQ
Advocate Advocate
1,886 Views
11 Replies
Message 1 of 12

VBA Module Naming Best Practices

eric.frissell26WKQ
Advocate
Advocate

Hi guys, I'm more or less self taught in VBA and have written dozens of scripts over the last 3 years.  One of the things I've found is that as your writing speed increases it seems the number of subs you write seems to be the square of your increase... in short, I've got a lot of modules in Inventor and have been working to organize them to reduce copy/pasting of code to help make a more dynamic environment.  As of a week ago the list of modules in Inventor was simply 'Module 1, Module 2, etc... " and I decided it would be a good idea to start renaming the modules for the function they're used for.  While this should make life a little easier in some aspects it actually screwed up the implementation of macros in the toolbar quite a bit... Posted is a snip of the now-renamed modules in the ApplicationProjects and after that is the toolbars customization to add the macros to the toolbar which you can see is essentially the modules in ApplicationProjects except doubled.  My first question is if there's a way to un-dock(?) the macros from the toolbars customization and then re-dock(?) them so that the list of macros only shows what's in the ApplicationProjects as opposed to what is in and what used to be?

 

Secondly some subroutines are simply copies of previous subroutines with certain options enabled or commented out and wanted to make sure the proper/sensible way to do this is to re-write all those options as private subs within one public sub so that you're just calling that option in different subs?

 

Lastly I assume that you generally want to name your module at inception so you don't end up in situations like the toolbar customization?  Doesn't seem to make much sense having module1, module2 when you have the option to rename them...

 

ericfrissell26WKQ_0-1644500288673.png

 

ericfrissell26WKQ_1-1644500448600.png

 

 

0 Likes
Accepted solutions (2)
1,887 Views
11 Replies
Replies (11)
Message 2 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Hi @eric.frissell26WKQ.  Great questions.

Module Names:

  After my first few VBA macros years ago, and seeing their names listed in the macros list as 'module name_Sub name', I have made it my personal practice to always name the Module exactly the same as my main/top Sub inside that module, right from the start.  This helped me to avoid later confusions when calling the macros to run, and when creating buttons for them in the toolbar.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 12

eric.frissell26WKQ
Advocate
Advocate

Thanks @WCrihfield, appreciate a lot of the work you've done in VBA for these forums.  Any chance you'd know how to remove the un-used macros from the customize toolbar menu?

0 Likes
Message 4 of 12

WCrihfield
Mentor
Mentor

That is a far more complex situation.  You can't just 'un-dock' or 're-dock' them, as you previously asked.  They are most likely within a RibbonPanel, which is within a RibbonTab, which is within a Ribbon, which may be specific to an Environment.  If tackling this by code, you could have to step down through all that to find the individual CommandControl objects which represent the buttons, then try to check its ControlDefinition.  I honestly have not tried to check for non-functional or broken macros behind buttons by code before, but I don't think it would be fun.  Manually, you could just use the usual Tools tab > Options panel > Customize > Ribbon tab dialog to remove the old/broken ones, then drag new ones over from the Macros filter on the left.  But if you have lots of tabs customized in lots of environments, it could take a long time to update it all.

 

Then there are the custom button images to deal with, if you created any of them.  Since they are very specifically named, you would most likely have to go through and rename those too, so they will be recognized and be paired with the macro buttons again.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 12

eric.frissell26WKQ
Advocate
Advocate

Don't need it by code, just a way to remove the broken ones from the available selection.  You'll notice that changing up the name doubled up the available macros in the customize tab so I'd like to remove the broken ones rather than having them appear as available when they aren't.

 

Otherwise next option is to create a new set .ivb file with the updated macros and remove the old one and pray that the customization tab updates to reflect the change as opposed to keeping the old list of macros and adding new ones.

0 Likes
Message 6 of 12

WCrihfield
Mentor
Mentor

I found that when I change the name of a Module that had a Macro within it listed in the customize list of macros, and that causes there to then be two of that same named macro to be listed within the customize list of macro, all I have to do is close Inventor, then reopen it, then when I check that list, it is only listed once.  So, it's just a session memory issue.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 12

eric.frissell26WKQ
Advocate
Advocate

Crazy, did that a few times now, even checked in and re-got the file from vault and it's still doubled up.  Guess next step is a full computer restart.

0 Likes
Message 8 of 12

eric.frissell26WKQ
Advocate
Advocate

Did a full computer restart and list is still showing a doubled up list.  This is Inventor 2020 in case there was a change

0 Likes
Message 9 of 12

WCrihfield
Mentor
Mentor

I'm currently using 2022.1.1.  I attempted to comb through the What's New, Fixed Issues, and Known Issues pages for the 2021 & 2022 and all the little .1, .2 updates to see if I could find something like this mentioned.  Although I saw VBA stuff mentioned several times, I did not see anything about this specific issue.  But sometimes it just says something like 'improved stability', which is pretty vague.  So I don't know if it is just a bug in that version/release, or something else.

What's New in Inventor 2021 

Inventor 2021 Release Notes 

Inventor 2021 Fixed Issues 

What's New in Inventor 2022 

Inventor 2022 Fixed Issues 

2022.1 Update Fixed Issues 

2022.2 Update Fixed Issues  

 

Was renaming the Modules the only thing you did?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 12

eric.frissell26WKQ
Advocate
Advocate
Accepted solution

For anyone who gets into the situation the solution is to have a blank second ivb file and then go to default VBA projects and set it to the second ivb.  The buttons for the first ivb file will remain until open the second ivb file in the customize window.  At that point it'll recognize there are no macros and everything will be removed.  Go back and reset your default vba projects to the first and everything will be back to normal

Message 11 of 12

eric.frissell26WKQ
Advocate
Advocate

Thanks for the help, was able to do this with changing the default vba project to a blank one and then switching it back.  You lose your buttons but the plus side is you know which macros are working now

0 Likes
Message 12 of 12

eric.frissell26WKQ
Advocate
Advocate

Just a quick update to this - the macro list started doubling up again so the above appears to have failed.  Only option left is I've copied/pasted all my codes to text documents and will be deleting the the macros and re-creating them in a new file.

 

If any Inventor people see this, it is a really annoying shortcoming that the list appears to be dynamic ONLY in adding the files but does not remove them.  If I've made a mistake in the way I've written these then I apologize and would really appreciate some advice on this situation

0 Likes