4 Replies Latest reply on Jun 13, 2014 6:36 AM by Darren Smith

    Notification when user closes a file?

    David Watkins

      I have been looking for a way in VBA to get a notification of the file that a user just closed or is trying to close (similar to how when you try to close an unsaved file and are asked if you would like to save the file). I was thinking that FileCloseNotify would work but sadly it seems that only works if you call closeandreopen which I am not. I have been working on this all day and not much to show for it so a point in the right direction would be great!

      Thanks,

      David

        • Re: Notification when user closes a file?
          Josh Brady

          I'm guessing you may want to look at the DestroyNotify for each individual document.  Not sure though.

          • Re: Notification when user closes a file?
            Artem Taturevych

            DPartDocEvents_DestroyNotify2EventHandler

            DAssemblyDocEvents_DestroyNotify2EventHandler

            DDrawingDocEvents_DestroyNotify2EventHandler

             

            But you need to setup the documents map. As Josh said this is the event for individual files. So you need to handle the DSldWorksEvents_DocumentLoadNotify2EventHandler to add the document to the map. Take a look at EventsHandling.vb file generated by add-in which shows how this should be setup. You may need to port this to VBA.

            ____________________________________________________

            Regards,

            Artem Taturevych, Application Engineer at Intercad (Australia)

             

            translationXpert – add-in to translate SolidWorks models

            myIntercad an integrated tool for SolidWorks Professionals

            LinkedIn SolidWorks API Education Group

              • Re: Notification when user closes a file?
                David Watkins

                Thanks Artem for the tip about the estroyNotify2 events. I think I finally understand how events work. I do not understand or know how to setup a document map or to use handle ( the best I can tell, handles are available in VB.NET but not VBA.) but I did come up with something that works. The code below is triggered any time the active doc is changed and a msgbox is sent when the doc is being closed just as a test.

                My main Module:

                 

                Public FileCloseListener As New FileCloseClass


                Sub main()

                FileCloseListener.MonitorSolidWorks

                End Sub

                 

                 

                My FileCloseClass :

                 

                Dim WithEvents swApp As SldWorks.SldWorks
                Dim WithEvents swAssem As SldWorks.AssemblyDoc
                Dim WithEvents swPart As SldWorks.PartDoc
                Dim WithEvents swDrawing As SldWorks.DrawingDoc
                Dim swModel As SldWorks.ModelDoc2

                Public Sub MonitorSolidWorks()

                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc


                End Sub

                Private Function swApp_ActiveDocChangeNotify() As Long
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc

                Dim OpenFile As String
                Dim OpenFileSplit() As String
                OpenFile = swModel.GetPathName


                OpenFileSplit = Split(OpenFile, ".")

                Select Case OpenFileSplit(1)
                    Case "sldprt", "SLDPRT"
                        Set swPart = swModel
                    Case "sldasm", "SLDASM"
                        Set swAssem = swModel
                    Case "slddrw", "SLDDRW"
                        Set swDrawing = swModel
                End Select

                End Function


                Private Function swPart_DestroyNotify2(ByVal DestroyType As Long) As Long
                    MsgBox "This is closing " & swModel.GetPathName & "!"
                End Function


                Private Function swAssem_DestroyNotify2(ByVal DestroyType As Long) As Long

                    MsgBox "This is closing " & swModel.GetPathName & "!"
                End Function

                Private Function swDrawing_DestroyNotify2(ByVal DestroyType As Long) As Long

                    MsgBox "This is closing " & swModel.GetPathName & "!"
                End Function