移交存储选定列表项的变量

Handing over variable storing selected list item

我可能只是错过了一个愚蠢的小细节,但我没有掌握它。

我创建了一个带有列表框的用户表单,我希望用户在其中 select 一个项目。就我的变量 "termin" 在我关闭用户表单

之前具有正确的值而言,这是有效的
Private Sub OKButton_click()
termin = Eventlist.List(Eventlist.ListIndex)
MsgBox termin             'Just for testing purposes. It gives me the selected item
Unload Eventabfrage
End Sub

这是我在 'ThisOutlookSession':

中的一部分
Option Explicit
Dim termin As String

Public Sub MailMerge()

    Eventabfrage.Show
    MsgBox termin   'and there it is empty but shouldn't be empty
enter code here

End Sub

我需要做什么才能将值移交给我的 MailMerge Sub? 它是 Outlook 中的宏,因此无法将其存储在任何 Excel 单元格中。

除了使用 Public 变量(我只是最后一次机会,因为它容易出现许多缺点),您可以使用 UserForm class Tag 属性 将信息传入和传出一个Userform实例和一个更安全的Userform实例化和终止方案,如下:

在调用模块中:

Public Sub MailMerge()
    Dim termin As String ' declare 'termin' as a Sub scoped variable

    With New Eventabfrage ' get a new instance of the wanted userform class and reference it

        .Show

        termin = .Tag ' retrieve referenced userform 'Tag' property and store it in 'termin' variable

        'enter code here  
    End With ' <-- this will unload the userform instance

End Sub

Eventabfrage class 模块中

Private Sub OKButton_Click()
    With Me ' reference the Userform class current instance
        .Tag = .Eventlist.List(.Eventlist.ListIndex)
        .Hide ' hide the userform instead of unloading it, so as to have it "alive", along with its properties (and methods) in its calling sub
    End With
End Sub