Outlook vb.net 如何处理 SentItems 文件夹的 ItemAdd 事件
Outlook vb.net how to handle the ItemAdd event for the SentItems folder
我正在尝试处理在 VB.net vsto 加载项中将新项目添加到 SentItems 文件夹时触发的 ItemAdd 事件。当我尝试这个时:
Private WithEvents mySentItems As Outlook.Items
mySentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
我在第二行收到 Declaration Expected 错误,我觉得这很奇怪,因为我以为我只是声明了它。
如果我这样做:
Private WithEvents mySentItems As Outlook.Items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
加载项编译但 outlook 变得非常生气,甚至在抛出此异常后不会加载加载项:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
System.NullReferenceException: Object reference not set to an instance of an object.
我在 ThisAddin class 声明之后,在任何 class subs 被声明之前做所有这些。
感谢您提供的任何帮助。
您似乎不能在子函数或函数之外进行赋值。我移动了行
mySentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
到 ThisAddin_Startup sub 并且它按预期工作。
谢谢你调查这个问题,老实说,我这样做不只是为了 post 我自己的答案,在我终于问这个问题之前,我已经解决了一天.
在初始化所有 Outlook 对象后,您可以在代码中使用以下声明,看起来像 属性 或方法 returns null (Nothing in VB.NET):
Private WithEvents mySentItems As Outlook.Items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
还有一个方面就是单行代码中使用了多个点。很难理解 属性 或方法究竟触发了异常,直到您打破链条并在一行代码中声明每个 属性 和方法。
此外,您不会立即释放底层 COM 对象。使用 System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects 文章。
我正在尝试处理在 VB.net vsto 加载项中将新项目添加到 SentItems 文件夹时触发的 ItemAdd 事件。当我尝试这个时:
Private WithEvents mySentItems As Outlook.Items
mySentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
我在第二行收到 Declaration Expected 错误,我觉得这很奇怪,因为我以为我只是声明了它。
如果我这样做:
Private WithEvents mySentItems As Outlook.Items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
加载项编译但 outlook 变得非常生气,甚至在抛出此异常后不会加载加载项:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
System.NullReferenceException: Object reference not set to an instance of an object.
我在 ThisAddin class 声明之后,在任何 class subs 被声明之前做所有这些。
感谢您提供的任何帮助。
您似乎不能在子函数或函数之外进行赋值。我移动了行
mySentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
到 ThisAddin_Startup sub 并且它按预期工作。
谢谢你调查这个问题,老实说,我这样做不只是为了 post 我自己的答案,在我终于问这个问题之前,我已经解决了一天.
在初始化所有 Outlook 对象后,您可以在代码中使用以下声明,看起来像 属性 或方法 returns null (Nothing in VB.NET):
Private WithEvents mySentItems As Outlook.Items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items
还有一个方面就是单行代码中使用了多个点。很难理解 属性 或方法究竟触发了异常,直到您打破链条并在一行代码中声明每个 属性 和方法。
此外,您不会立即释放底层 COM 对象。使用 System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects 文章。