Outlook 2013 VBA Public 变量不存在

Outlook 2013 VBA Public Variable Doesn't Persist

在 ThisOutlookSession 的顶部我定义:

Option Explicit
Public varTest As Long

我在 ThisOutlookSession 中也有几个测试程序。

使用调试 I 运行 这个 test3 程序:

Sub test3()
  varTest = 42
End Sub

然后我 运行 test3b,但是 varTest 显示的不是值 42,而是它的值显示为零。

Sub test3b()
  MsgBox varTest
End Sub

显然我对范围或持久性有一些误解。我做错了什么?

经过多次搜索,我发现 Sue Mosher, Outlook MVP 如下:

Remember that ThisOutlookSession is a class module.

You need to add at least one regular code module to the project and declare your global variables in it, not in ThisOutlookSession, by using the Public keyword instead of plain old Dim

果然,当我将 Public 变量定义移动到模块时,持久性问题就解决了。