MACRO LIBRARY

Cancel save if part wasn't modified using notifications

Free public access. No account or subscription required.

Files

Cancel save if part wasn't modified using notifications

Code

Modules: Part__Notify__Returning

'This macro listens for File-->Save As. When notification is fired, macro examines _
    whether any changes have been made to the model since the listener was turned on. _
    If not, then Save As is cancelled.
'Preconditions: Part is open
'Postconditions: See immediate window
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim save_as_listener As clsSaveAsListener
    
Sub main()
    Set swApp = Application.SldWorks
    Set save_as_listener = New clsSaveAsListener
    save_as_listener.connect swApp
End Sub

Class Modules: clsSaveAsListener

Dim swModel As SldWorks.ModelDoc2
Dim WithEvents swPart As SldWorks.PartDoc
Dim intTimeStamp As Integer
    
Public Sub connect(swApp As SldWorks.SldWorks)
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    Debug.Print "listener on"
    intTimeStamp = swModel.GetUpdateStamp
End Sub
    
Private Function swPart_FileSaveAsNotify2(ByVal FileName As String) As Long
    If intTimeStamp = swModel.GetUpdateStamp Then
        swPart_FileSaveAsNotify2 = 1    'for some reason, S_FALSE does not work
        Debug.Print "Save will not proceed because no change made to file."
    Else
        Debug.Print "Save will proceed because changes made to file."
    End If
End Function
    
'Notes: For return values, S_FALSE = 1, S_OK = 0