Macro with multiple subs without references.

Programming and macros
User avatar
Hansjoerg
Posts: 106
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 72
x 55

Macro with multiple subs without references.

Unread post by Hansjoerg »

Again something learned. o[
I just finished a new macro, where I worked for the first time with multiple subs to structure the code a bit. One of the additional subs has no reference
The macro worked without problems in the editor. If the mouse pointer did not point to the sub main, I had to select in the popping up window which sub should be executed.

The tide turned when I tried to start the macro with the execute button. Then nothing happened!

A post in the good old forum brought me to the right track: https://forum.solidworks.com/thread/226354

In my ignorance I assumed that the execute button always calls the sub main. But that is probably not so!

If you create your own button for the macro, you can specify the start procedure. But what do you do if you have macros that are only started by the execute button?

Add a reference to all additional subs, or change the subs without references to function, so that only the sub main is left to start?

Or are there other solutions? What is the clean way?
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
User avatar
josh
Posts: 249
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 440

Re: Macro with multiple subs without references.

Unread post by josh »

I like to add optional arguments that don't do anything. Like:

Sub NotMain(Optional NotMeDummy as Long)

This will keep the execute button from calling them, and also prevent some user from mapping the macro button incorrectly.

Multiple options for start routine are actually useful sometimes. It allows you to have one macro with multiple different start options. For example, I have a macro that moves windows right and left inside SW. Most of the code to find windows, measure desktop, etc. is all common whether the window needs to move right or left. So I call two different start routines within the same macro depending on which way the window needs to move. If I need to modify the common code I only need to do it once.
User avatar
AlexB
Posts: 434
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 21
x 240
x 383

Re: Macro with multiple subs without references.

Unread post by AlexB »

Like you already mentioned, my go to method is to just make everything but the "main" a function that returns a boolean or something. If I use the value at all, I'd use that to tell me if the function completed with or without error.
Post Reply