MACRO LIBRARY

Determine whether new or existing file was opened

Free public access. No account or subscription required.

Files

Determine whether new or existing file was opened using notifications

Code

Modules: notifications1

'This macro demonstrates basic notifications. In the main module, we simply create _
    a global variable that stays alive indefinitely, until stop is pressed in the _
    VB Editor or the global variable is set to nothing. As long as it is alive, the _
    global variable is "listening" for certain events.
'This macro in particular indicates when a new or existing file is opened. If the _
    former, the type is indicated. If the latter, the file name and type are indicated.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Public listener As clsListener
    
Sub main()
    Set listener = New clsListener
    listener.connect
End Sub

Class Modules: clsListener

Dim WithEvents swApp As SldWorks.SldWorks
    
Public Function connect() As Boolean
    Set swApp = Application.SldWorks
    Debug.Print "listening started"
End Function
    
Private Function swApp_FileNewNotify2(ByVal newDoc As Object, ByVal DocType As Long, ByVal TemplateName As String) As Long
    Select Case DocType
        Case swDocumentTypes_e.swDocASSEMBLY
            Debug.Print "Document type opened: Assembly"
        Case swDocumentTypes_e.swDocDRAWING
            Debug.Print "Document type opened: Drawing"
        Case swDocumentTypes_e.swDocPART
            Debug.Print "Document type opened: Part"
        Case Else
            Debug.Print "Document type opened: Unknown"
    End Select
    Set listener = Nothing
    Debug.Print "listening stopped"
End Function
    
Private Function swApp_FileOpenPostNotify(ByVal FileName As String) As Long
    Debug.Print FileName & " opened."
    Select Case UCase(Right(FileName, 6))
        Case "SLDASM"
            Debug.Print "Document type opened: Assembly"
        Case "SLDDRW"
            Debug.Print "Document type opened: Drawing"
        Case "SLDPRT"
            Debug.Print "Document type opened: Part"
        Case Else
            Debug.Print "Document type opened: Unknown"
    End Select
    Set listener = Nothing
    Debug.Print "listening stopped"
End Function