15 Replies Latest reply on Apr 14, 2011 5:03 PM by Jacob Cordingley

    Run macro when saving

    Adam Meyer
      Hey all,

      I'm a newbie when it comes to macros, and the search feature doesn't work, so I have to ask.

      Can someone show me how to get a macro to run when saving a file in Solidworks. I have the macro, but I'd like to modify it so that it runs when saving a drawing.

      Thanks,

      Adam
        • Run macro when saving
          Josh Brady
          It's not particularly easy to do, and not really best to do with macros either. If possible, it is better to delete your save button off the toolbar and replace it with a macro button.
          • Run macro when saving
            Adam,

            Other option is to have the macro running in the background and have it monitor when the users presses the save button it would run your code. It has been a while since I did something like this, so I will have to look at the proeedure to do it.
              • Run macro when saving
                Adam Meyer

                Wayne Matus wrote:

                 

                Adam,



                Other option is to have the macro running in the background and have it monitor when the users presses the save button it would run your code. It has been a while since I did something like this, so I will have to look at the proeedure to do it.

                Any help is appreciated.
              • Run macro when saving
                Tobin Sparks
                Howdy adam,

                The attached file and .bmp might help.
                It contains instructions for adding a button and a sample button you might be able to use.

                  • Run macro when saving
                    OK, here is the macro. You will need to edit and place your code in the preSave sub on the Class1 module.

                    The macro stays running in the background, so you run it once and it stays running until you close SolidWorks. It will run your code just before the active document is actually saved.
                      • Run macro when saving
                        Steve Hall
                        Downloaded the macro. When you say ' you will need to edit and place code in the preSave...' you lost me. Macros I understand, but Visual Basic is greek. Could you be more specific. I'm trying to get it to prompt the File Properties input box upon clicking the Save button.
                        • Re: Run macro when saving
                          Henrik Persson

                          Hi,

                          If i want to run another marco when saving how do i write this? Can you call another macro or should this be included in the running macro?

                            • Re: Run macro when saving
                              Filipe Venceslau

                              You can do either...but it might be best to call a macro instead.

                              Paste this procedure (adjust your arguments to your needs first) in the macro that will be triggering the macro you want to run...then call the procedure whenever you want to run the macro.

                               

                               

                              RunMacro2 Method (ISldWorks)

                               

                              Sub RunMyMacro()

                                   Set swApp = Application.SldWorks
                                   Dim runMacroError As Long
                                   boolstatus = swApp.RunMacro2("c:\myMacro.swp", "StartModuleName", "StartProcedureName", swRunMacroOption_e.swRunMacroUnloadAfterRun, runMacroError)

                              End Sub

                               

                              Hope this helps,

                               

                              Cheers

                               

                              Filipe

                        • Run macro when saving
                          Steve, I do not think you can launch the File Properties dialog box through the API. You would have to write your own or find one that someone else already has that would allow the user to see, change, and add custom properties.
                            • Run macro when saving
                              Steve Hall
                              What if I want to just edit a text box or just set the variable SW-Author?
                              • Run macro when saving
                                Josh Brady

                                Wayne Matus wrote:

                                 

                                Steve, I do not think you can launch the File Properties dialog box through the API. You would have to write your own or find one that someone else already has that would allow the user to see, change, and add custom properties.

                                Sure you can. It's one of the available options for SldWorks::RunCommand. The following line will open the custom properties dialog:

                                Application.SldWorks.RunCommand swCommands_File_Summaryinfo, ""
                              • Run macro when saving
                                Yes, that can be done. What you would do is look in the Class1 module and find the sub

                                Sub preSave(sFileName As String)
                                MsgBox "About to save " & sFileName
                                End Sub

                                You would replace the MsgBox "About to save " & sFileName with your macro that bring up text box to set the SW-Author.
                                • Run macro when saving
                                  Thanks, Josh.

                                  It does look like that still does not work like it should with the file save notification. The file gets saved in the background while the dialog box is open. So you would have to save twice for it to work.
                                  • Re: Run macro when saving
                                    David Marsland

                                    The best way to do this is to monitor for save Events. To do this you need to add a form into your macro (a blank one that's never seen):

                                     

                                    Fist start your macro in a module, and initiate the form, but don't show it:

                                     

                                         Sub Main()

                                              YourForm.Initiate

                                         End Sub

                                     

                                    In the form declare that you want to run it WithEvents and decalre an object with Events, like a model or drawing:

                                     

                                         Public WithEvents swApp As SldWorks.SldWorks

                                         Dim WithEvents swDrawing As SldWorks.DrawingDoc

                                     

                                    Then add a function to monitor a specific event, such as saving:

                                     

                                          Private Function swDrawing_FileSaveNotify(ByVal FileName As String) As Long

                                              Msgbox "Saving now..."

                                          End function

                                     

                                    If you want the code to run after saving instead of before, use *_FileSavePostNotify

                                     

                                    And that's it. Enjoy.

                                    • Re: Run macro when saving
                                      Jacob Cordingley

                                      create an addon insteed

                                      it almost like a marco

                                      then on the save or saveas event have your code

                                      there are Visual studio templates to do this fast or look at  Luke Malpass website to do this