Search code examples
vbaautocad

Set the return of a function to another module variable


    '' Module1 Function    
    Public Function AddSelectionSet(ssName As String) As AcadSelectionSet
            On Error Resume Next
            Dim ss As AcadSelectionSet
            Set ss = ThisDrawing.SelectionSets.Add(ssName)
            If Err.Number <> 0 Then
                Set ss = ThisDrawing.SelectionSets.Item(ssName)
            End If
        End Function

''Module2 code
Dim mySS As AcadSelectionSet
Set mySS = AddSelectionSet "myName"

Both above and below code results in "Syntax error" from AutoCAD's VBAIDE.

Set mySS = Call Module1.AddSelectionSet "myName"

Solution

  • Your function never returns anything....

    You need Set AddSelectionSet = ss so that it will return your ss

    Public Function AddSelectionSet(ssName As String) As AcadSelectionSet
      Dim ss As AcadSelectionSet
    
      On Error Resume Next
      Set ss = ThisDrawing.SelectionSets.Add(ssName)
      If Err.Number <> 0 Then Set ss = ThisDrawing.SelectionSets.Item(ssName)
    
      Set AddSelectionSet = ss
    End Function
    

    And then to use the function:

    Dim mySS As AcadSelectionSet
    Set mySS = AddSelectionSet("myName")