Micro won't run on first button press if...

We have a bunch of macros that are run from a “Run Macro” button that the user has put on a toolbar or from a button on a VBA form. We have noticed that if a cell in a table is preselected when a run macro button is pressed, the macros doesn’t run. If the button is then pressed a second, third, etc. time, it does run normally. This isn’t true if a whole table, row, or column is preselected. It’s only when a cell for set of cells in a table is selected.

We have noticed this behavior for years over serveral different versions of SolidWorks. I can’t imagine that we are the only ones seeing this behavior. Wondering if there is a way to work around it. It’s not preventing our macros from working. We just have to hit buttons twice to make things work.

Usually when this happen, it is something with the macro codes. So can you share one macro with the test files to check?

It happens with every VBA macro. Here’s an example.

1. Make a macro with the following code.
2. View the Immedaite Window in the IDE.
3. Open any drawing that has a table in it.
4. Select a cell in a table.
5. Run the macro using any method you wish (from the IDE, from a "new macro" button, from the Tools>Macro>Run menu, etc.
6. Notice nothing will be output to the Immediate Window.
7. Run the macro again.
8. "Hello World" will now be output to the Immediate Window.

Sub main()
    Debug.Print "Hello World"
End Sub

It happens in SolidWorks when editing a gauge table while editing an excel cell. I believe it is a ‘limitation’, not sure. Perhaps there could be a check-in to existing microsoft office applications to end all current commands prior to launching the macro..?

I’m not sure what you mean. I’d love to look into that if there’s a potential solution there. Can you elaborate for me?

Thank You

I’m not really a programmer, I’m just spitballing here thinking that’s a plausible solution because I thought it was a possible route, perhaps I am wrong.

If the problem is that SW isn’t running the macro because the focus is elsewhere, there’s nothing you can do in the macro to solve this problem.

I do see this behavior as well. In the screenshot here, one cell of the table is selected. Not only can macros not be run with one click, most toolbar buttons will not do anything on the first click. Interestingly, the Selection Filter buttons do still work on the first click, I didn’t find any others. Same as the OP, if a row or column is selected, this doesn’t happen. It seems that the popup toolbar has the “focus” when a cell is selected, but not when a column or row is selected.

Anyway, as Mr. Perman stated above, there’s nothing you can do about this. It’s just how SolidWorks works (or doesn’t works).
image.png