MACRO LIBRARY

Lesson 3.1 homework part C solution

Free public access. No account or subscription required.

Files

Lesson 3.1 homework part C solution

Code

Modules: HW_1c1

'Homework 3.1c
'Create a macro using UserForm1, in which the user fills the list box with the names of the _
    currently open documents and then can select one of the items in the list, click the _
    Activate button, and that document is activated.
    
Sub main()
    UserForm1.Show
End Sub

Forms: UserForms1

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vDocs As Variant
Dim strSelectedDoc As String
Dim lngErrors As Long
    
Private Sub cmdActivate_Click()
    If lstOpenDocs.ListIndex <> -1 Then
        strSelectedDoc = lstOpenDocs.Value
        Set swModel = swApp.ActivateDoc2(strSelectedDoc, True, lngErrors)
        If swModel Is Nothing Then MsgBox "Model not open. Activation failed."
    End If
End Sub
    
Private Sub cmdGetOpen_Click()
    lstOpenDocs.Clear
    vDocs = swApp.GetDocuments
    If IsEmpty(vDocs) = False Then
        For i = 0 To UBound(vDocs)
            Set swModel = vDocs(i)
            lstOpenDocs.AddItem swModel.GetTitle
        Next i
    End If
End Sub
    
Private Sub UserForm_Initialize()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
End Sub