Determine the type Of active document using SOLIDWORKS API

Edit ArticleEdit Article

This example displays the message box of the type of the document currently active in SOLIDWORKS. This will work regardless the document is saved or not. IModelDoc2::GetType SOLIDWORKS API method can be used to return the type enumeration which will identify the document as SOLIDWORKS Part, Assembly or Drawing.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If Not swModel Is Nothing Then
        Select Case swModel.GetType
            Case swDocPART:
                MsgBox "Active document is Part"
            Case swDocASSEMBLY:
                MsgBox "Active document is Assembly"
            Case swDocDRAWING:
                MsgBox "Active document is Drawing"
        End Select
        MsgBox "No document opened"
    End If
End Sub


All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.

Product of Xarial Product of Xarial