More 'Goodies'

Add-ins list in SOLIWORKS menu
Add-ins list in SOLIWORKS menu

This macro allows to trigger the load state of the specified add-in using the ISldWorks::LoadAddIn and ISldWorks::UnloadAddIn SOLIDWORKS API.

This can be useful to provide a short-cut for loading and unloading the add-in with one button click. It is recommended to use Macro Buttons to create a button for add-in in the toolbar.

Macro requires the add-in Global Unique Identifier (GUID) to be specified at the beginning of the macro.

Const ADD_IN_GUID As String = "{1730410d-85ad-4be8-aa2d-ed977b93fe5d}"

Locate the guid of the required SOLIDWORKS add-in in the registry at HKLM\SOFTWARE\SolidWorks\AddIns. Each sub-key of this registry key represents the add-in. Select each key to see the title and description of the add-in. Copy the name of the key which represents the add-in guid.

Available add-ins presented in the registry
Available add-ins presented in the registry
Const ADD_IN_GUID As String = "{1730410d-85ad-4be8-aa2d-ed977b93fe5d}"

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    Dim isLoaded As Boolean
    isLoaded = IsAddInLoaded(ADD_IN_GUID)
    Debug.Print "AddIn Loaded: " & isLoaded
    Dim addInPath As String
    addInPath = GetAddInPath(ADD_IN_GUID)
    Debug.Print LoadAddIn(addInPath, Not isLoaded)
End Sub

Function IsAddInLoaded(addInGuid As String) As Boolean
    Dim addIn As Object
    Set addIn = swApp.GetAddInObject(addInGuid)
    IsAddInLoaded = Not addIn Is Nothing
End Function

Function LoadAddIn(addInGuid As String, load As Boolean) As Boolean
    Const SUCCESS As Long = 0
    Dim res As Long
    If True = load Then
        res = swApp.LoadAddIn(addInGuid)
        res = swApp.UnloadAddIn(addInGuid)
    End If
    LoadAddIn = (res = SUCCESS)
End Function

Function GetAddInPath(addInGuid As String) As String
    Dim addInUri As String
    addInUri = CreateObject("WScript.Shell").RegRead("HKCR\CLSID\" & addInGuid & "\InprocServer32\CodeBase")
    GetAddInPath = UriToLocalPath(addInUri)
End Function

Function UriToLocalPath(uri As String) As String
    UriToLocalPath = Right(uri, Len(uri) - Len("file:///"))
    UriToLocalPath = Replace(UriToLocalPath, "/", "\")
End Function