How to Run Macros While Closing the Solidworks)

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
senuba
Posts: 16
Joined: Thu Oct 26, 2023 1:37 am
Answers: 0
x 16
x 6

How to Run Macros While Closing the Solidworks)

Unread post 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: .
User avatar
SPerman
Posts: 2056
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2227
x 1878
Contact:

Re: How to Run Macros While Closing the Solidworks)

Unread post 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.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
senuba
Posts: 16
Joined: Thu Oct 26, 2023 1:37 am
Answers: 0
x 16
x 6

Re: How to Run Macros While Closing the Solidworks)

Unread post 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.
User avatar
SPerman
Posts: 2056
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2227
x 1878
Contact:

Re: How to Run Macros While Closing the Solidworks)

Unread post 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.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
JSculley
Posts: 646
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 878

Re: How to Run Macros While Closing the Solidworks)

Unread post 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.
senuba
Posts: 16
Joined: Thu Oct 26, 2023 1:37 am
Answers: 0
x 16
x 6

Re: How to Run Macros While Closing the Solidworks)

Unread post 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.
Post Reply