【问题标题】:Set the return of a function to another module variable将函数的返回设置为另一个模块变量
【发布时间】:2022-01-14 18:35:55
【问题描述】:
    '' 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"

【问题讨论】:

  • 不是 AutoCAD 人员,但请尝试Set mySS = AddSelectionSet("myName")
  • 你的函数永远不会返回任何东西......你需要Set AddSelectionSet = ss以便它返回你的ss,并且你的module2代码需要在"myName"部分周围加上括号。
  • 谢谢@braX 和@BigBen!在 Module1 上添加了 Set AddSelectionSet = ss,然后:Set mySS = AddSelectionSet("myName")。在 Module2 上工作。

标签: vba autocad


【解决方案1】:

你的函数永远不会返回任何东西......

您需要Set AddSelectionSet = ss,以便它返回您的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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-25
    • 2022-01-22
    • 2021-07-02
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多