将函数的 return 设置为另一个模块变量

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"

上面和下面的代码都会导致 AutoCAD 的 VBAIDE 出现“语法错误”。

Set mySS = Call Module1.AddSelectionSet "myName"

你的函数从来没有return任何东西....

您需要 Set AddSelectionSet = ss 以便它 return 您的 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

然后使用函数:

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