9 Replies Latest reply on Sep 15, 2009 9:02 AM by Roland Schwarz

    OnSave event



      I'm trying to get some code to run whenever a document is saved. Can anyone advise?



        • Re: OnSave event
          Artem Taturevich

          Catch one of the following events:







          for Part, Assembly and Drawing documents.

            • Re: OnSave event

              Nice one Ta.


              How exactly do I use this to monitor for any save event, at any time, and keep monitoring ? I've found examples based on forms that monitor for any save event, but the functinon part of the code is not clear at all. All I want is an example that could (for example) display a msgbox on the FileSavePostNotifyEvent.

                • Re: OnSave event
                  Artem Taturevich



                  Please find a sample macro in the attachment.

                  • Re: OnSave event
                    Josh Brady

                    Not sure what your experience level is with SW API and VBA or whatever language you intend to program in - sounds like you may be a bit of a novice?  I'm not trying to put you down or anything, just need to confirm where the starting point is.


                    Basically, this is likely going to be a bit more involved than it seems on the surface.  You need to know exactly when you want your code to run and what file you want to run it on.


                    All of the OnSave type events are fired for specific types of documents - PartDoc, AssemblyDoc, and DrawingDoc.  Your code will have to have a variable for whatever type of document you are trying to save.


                    If you want code to run whenever the active document is saved, you will need to have code in your macro that monitors the SldWorks application object for the ActiveDocChangeNotify event, and assign the new active document to the proper type of specific document object.


                    SolidWorks often saves a document that is not the active document.  For example, if you insert a part into an assembly, switch to the part, change it, then switch back to the assembly and save the assembly, the part document is automatically saved.  If you want to be notified any time any document open in SW memory is saved, you will have to get a variable for every single open document in SW, and manage them all.  That means cleaning them up when SW closes them, monitoring SW for any new documents that open, etc.  I've never really considered even trying to do this, but it may be possible.

                • Re: OnSave event
                  Deepak Gupta
                  Check the links suggested by Josh Brady   https://forum.solidworks.com/message/117597#117597
                  • Re: OnSave event
                    Roland Schwarz

                    Events are only available for use in class objects or in forms, not in modules.  You must either use a form or wrap your objects in a class module.


                    You need to use early binding and "Withevents" qualifier to define your object variables.

                    Dim WithEvents swApp as Sldworks.SldWorks
                    • Re: OnSave event
                      Got it! Cheers all