Page 1 of 1

How to Run Macros While Closing the Solidworks)

Posted: Tue Mar 26, 2024 9:16 am
by senuba
Hello everyone,
Sometimes when designing, I forget to run my important macros. Therefore, I came up with an idea and started researching: when finishing a drawing, I want a macro named Last.swp to run automatically when closing the part file. Can I do this, and do you have an example? oa

I found the "DestroyNotify2 of PartDoc" code but couldn't apply it. There could be another code as well; it doesn't have to be specifically this one for me.

Also, I can't add anything to the path section of the SolidWorks shortcut because it's a company computer, and some things are restricted :roll: .

Re: How to Run Macros While Closing the Solidworks)

Posted: Tue Mar 26, 2024 9:24 am
by SPerman
This is an old post from the old forums.

https://forum.solidworks.com/thread/81373

Artem Taturevych

Yes, it could be done. You need to catch the DestroyNotify2 of AssemblyDoc, PartDoc and DrawingDoc and call the SldWorks::RunMacro2 from the event handler.

Re: How to Run Macros While Closing the Solidworks)

Posted: Tue Mar 26, 2024 9:34 am
by senuba
SPerman wrote: Tue Mar 26, 2024 9:24 am This is an old post from the old forums.

https://forum.solidworks.com/thread/81373
Yes, I saw that page too, but I couldn't apply it. I quoted "DestroyNotify2 of PartDoc" from there. But I couldn't do it, I didn't fully understand.

Re: How to Run Macros While Closing the Solidworks)

Posted: Tue Mar 26, 2024 9:44 am
by SPerman
@artem is on these forums. Maybe he will reply and elaborate.

ETA: He was on the forums. It looks like he hasn't been active in over a year.

Re: How to Run Macros While Closing the Solidworks)

Posted: Tue Mar 26, 2024 11:00 am
by JSculley
senuba wrote: Tue Mar 26, 2024 9:16 am Hello everyone,
Sometimes when designing, I forget to run my important macros. Therefore, I came up with an idea and started researching: when finishing a drawing, I want a macro named Last.swp to run automatically when closing the part file. Can I do this, and do you have an example? oa

I found the "DestroyNotify2 of PartDoc" code but couldn't apply it. There could be another code as well; it doesn't have to be specifically this one for me.

Also, I can't add anything to the path section of the SolidWorks shortcut because it's a company computer, and some things are restricted :roll: .
You can't really create a macro to do this. You will just shift the problem because you will have to remember to run a macro every time you start SOLIDWORKS so that it can listen for the Destroy event and this macro would have to be pretty complicated because it has to keep track of every document you open and close. This is not a novice level programming task. You could potentially add a macro feature to your template files that would set up the event handlers, but that too isn't really a beginner level programming task. The best way to do this would be to create a SOLIDWORKS add-in, which is programmatically even more complicated.

Re: How to Run Macros While Closing the Solidworks)

Posted: Wed Mar 27, 2024 3:30 am
by senuba
JSculley wrote: Tue Mar 26, 2024 11:00 am You can't really create a macro to do this. You will just shift the problem because you will have to remember to run a macro every time you start SOLIDWORKS so that it can listen for the Destroy event and this macro would have to be pretty complicated because it has to keep track of every document you open and close. This is not a novice level programming task. You could potentially add a macro feature to your template files that would set up the event handlers, but that too isn't really a beginner level programming task. The best way to do this would be to create a SOLIDWORKS add-in, which is programmatically even more complicated.

I understand. Thank you. I'll consider another way then.