简体   繁体   English

Outlook vb.net如何处理SentItems文件夹的ItemAdd事件

[英]Outlook vb.net how to handle the ItemAdd event for the SentItems folder

I am trying to handle the ItemAdd event fired when a new item gets added to the SentItems folder in a VB.net vsto add-in. 我正在尝试处理将新项目添加到VB.net vsto加载项中的SentItems文件夹时触发的ItemAdd事件。 When I try this: 当我尝试这个:

Private WithEvents mySentItems As Outlook.Items
mySentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items

I get a Declaration Expected error on the second line, which I find super bizarre since I thought I JUST declared it. 我在第二行收到一个“声明期望”错误,由于我以为自己只是声明了它,所以我觉得这很奇怪。

If I do this: 如果我这样做:

 Private WithEvents mySentItems As Outlook.Items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items

The add-in compiles but then outlook gets really angry and won't even load the add in after throwing this exception: 加载项会编译,但是随后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.

I am doing all of this just after the ThisAddin class declaration before any of the class subs are declared. 我要在ThisAddin类声明之后,所有类子声明之前执行所有这些操作。

Thanks for any help you may be able to provide. 感谢您提供的任何帮助。

It seems you cannot make assignments outside of a sub or function. 看来您无法在子或函数之外进行分配。 I moved the line 我搬了线

mySentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items

to the ThisAddin_Startup sub and it worked as expected. 到ThisAddin_Startup子项,它按预期工作。

Thanks for looking into this if you did, and honestly I don't do this to just post my own answer, I hand't managed to solve this for a day before I finally asked the question. 谢谢您(如果您愿意)对此进行调查,说实话,我不会这样做只是为了发布自己的答案,在我最终提出问题之前的一天里,我都没有设法解决这一问题。

You can use the following declarations in the code after all Outlook objects are initialized, looks like a property or method returns null (Nothing in VB.NET): 初始化所有Outlook对象之后,可以在代码中使用以下声明,看起来像属性或方法返回null(VB.NET中为Nothing):

 Private WithEvents mySentItems As Outlook.Items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items

Another aspect is that multiple dots are used in the single line of code. 另一方面是在一行代码中使用了多个点。 It is hard to understand what property or method exactly fires an exception until you break the chain and declare each property and method on a single line of code. 在打破链条并在一行代码中声明每个属性和方法之前,很难理解什么属性或方法会确切地引发异常。

Moreover, you don't release underlying COM objects instantly. 此外,您不会立即释放基础COM对象。 Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. 使用完后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。 Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. 然后在Visual Basic中将变量设置为Nothing(在C#中为null)以释放对该对象的引用。 Read more about that in the Systematically Releasing Objects article. 在“ 系统发布对象”文章中了解有关此内容的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM