Outlook VBA中的WithEvents对象最终无法引发事件 [英] WithEvents object in Outlook VBA eventually fails to raise event

查看:220
本文介绍了Outlook VBA中的WithEvents对象最终无法引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,好像以前有人遇到过这个问题,但是我没有看到任何进一步的评论或解决方案.请参阅此问题的已接受答案中的编辑".

Okay, looks like someone has encountered this problem before, but I didn't see any further comments or solutions. See the Edit in the accepted answer to this question.

我的情况是这样的.

我正在使用Exchange电子邮件帐户在Win10 x64下运行Outlook 2013.有时我一次运行Outlook几天甚至一周甚至更长的时间,而没有关闭它.

I am running Outlook 2013 under Win10 x64 with an Exchange email account. I sometimes run Outlook for several days to a week or more at a time without closing it.

当新项目添加到已发送邮件"文件夹中时,我想引发一个事件.这需要在Application ItemSend事件之后发生,因为,例如,我想在消息发送后删除它,而在ItemSend事件处理程序中您无法执行此操作.

I want to raise an event when a new item is added to the Sent Mail folder. This needs to occur after the Application ItemSend event because, let's say, I want to delete the message after it is sent, which you cannot do from within the ItemSend event handler.

所以我有以下代码:

Public WithEvents goSent As Outlook.Items

Private Sub Application_Startup()

    'Establish the global object for the folder we want to monitor.

    Set goSent = Session.GetDefaultFolder(olFolderSentMail).Items

End Sub

Private Sub goSent_ItemAdd(ByVal Item As Object)

    'Do stuff here.

End Sub

一切正常工作一天或更长时间,但最终ItemAdd事件处理程序停止触发.如果我进入VBA编辑器并手动运行Application_Startup,它将重新开始工作.

Everything works beautifully for a day or longer, but eventually the ItemAdd event handler stops firing. If I enter the VBA editor and manually run Application_Startup, then it starts working again.

[Edit:]其他信息:无论Outlook运行了多长时间,内置"事件(如Application ItemSend)似乎总是可靠地触发.

One other bit of information: The "built-in" events, like Application ItemSend, always seem to fire reliably no matter how long Outlook has been running.

这次,在事件停止触发后,我放了一天时间.我打开了VBA编辑器,并在Application ItemSend过程中放置​​了一个断点.当它停止时,我查询了goSent对象,发现(a)它仍然存在(而不是Nothing),但是(b)它中仅包含昨天存在的项目,大概是当时已从已发送邮件"项目集合中变为不受限制".当我在即时"窗口中提交新的Set语句时,它立即又开始工作.

This time, I let things go for a day after the event stopped firing. I opened the VBA editor and put a breakpoint in the Application ItemSend procedure. When it stopped, I queried the goSent object and found that (a) it still existed (rather than being Nothing), but (b) it had only the items in it that were there yesterday, presumably at the time it became "untethered" from the Sent Mail items collection. When I submitted a new Set statement in the Immediate window, it immediately began to work again.

我注意到MSDN文档说要在类模块中放置自定义对象的事件处理程序,例如我的ItemAdd事件.我在ThisOutlookSession中拥有我的名字,但是据我了解,ThisOutlookSession 一个类模块.这有问题吗?

I noticed that the MSDN documentation says to put event handlers for custom objects--like my ItemAdd event--in class modules. I have mine in ThisOutlookSession, but it was my understanding that ThisOutlookSession is a class module. Is there a problem with that?

有人知道为什么会发生这种情况以及如何处理吗?我考虑过为Application ItemSend添加一个事件处理程序,并在每次触发时仅重新分配goSent对象,但这并不能解决潜在的问题.

Any idea why this would happen and what to do about it? I considered adding an event handler for Application ItemSend and just reassigning the goSent object every time that's fired, but it doesn't address the underlying problem.

一段时间以来,我在Application ItemSend处理程序中有一个Set语句,尽管它已经解决方法,而不是真正的解决方案.当我将已发送项目的交货延迟了很长时间时,它似乎失败了,并且在此期间我没有再发送任何消息了.然后goSent对象与已发送邮件"集合断开连接,该消息实际上是在此点之后和Application ItemSend再次触发之前从发件箱发送的.

For a while now, I've had a Set statement in the Application ItemSend handler, and that seems to have mostly taken care of things, even though it's a workaround, not really a solution. It appears to fail when I have delayed delivery on a sent item for an extended time, and I don't send any further messages in the meantime. Then the goSent object disconnects from the Sent Mail collection, and the message is actually sent from the Outbox after that point and before Application ItemSend fires again.

谢谢!

与原始问题无关:我发现我的宏无法使用处于在线模式的Exchange服务器来完成我想要的操作(即,非缓存模式),如果我的发件箱中的邮件传递延迟,并且传递时间过去后Outlook已关闭.在联机模式下,Exchange本身发送这些消息并将其添加到服务器上的已发送邮件"文件夹中,因此,当Outlook重新打开时,这些邮件已经在已发送邮件"集合中,并且不会触发任何事件.这是有道理的.请参阅讨论

Unrelated to original issue: I discovered that my macros fail to accomplish what I want with an Exchange server that is in online mode (i.e., not cached mode) if I have messages in the Outbox with delayed delivery and Outlook is closed when the delivery time passes. In online mode, Exchange itself sends these messages and adds them to the Sent Mail folder on the server, so when Outlook reopens, the messages are already in the Sent Mail collection and no event fires. Which makes sense. See discussion here. Looks like I would need to add something to my Application_Startup macro to look for messages sent since Outlook last closed.

推荐答案

我遇到了类似的问题,但从未找到合适的解决方案.我所做的是写了一个小批处理脚本,该脚本关闭并打开Outlook应用程序,然后在任务计划程序中设置一个任务,使其在工作时间以外的每个小时运行一次.您也可以在vbs中轻松做到这一点.

I had similar issue and never found a proper solution. What I did was I wrote a little batch script that closes and opens outlook app, then I set a task in task scheduler to run it every hour outside of my working hours. You can also do it easily in vbs.

这篇关于Outlook VBA中的WithEvents对象最终无法引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆