子完成后,Application_Startup变量消失 [英] Application_Startup variable disappears after sub finishes

查看:100
本文介绍了子完成后,Application_Startup变量消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对上一个问题的后续操作我问过.谢谢社区的帮助.

This is a followup to a previous question I had asked. Thank you to the community for your help with that.

我正在尝试首次创建WithEvents代码,以检查文件夹中是否有新项目.最终的计划是使用ItemsAdd事件来触发其他一系列处理,但就目前而言,只是尝试将其保存到文件夹中而已.

I'm trying to create WithEvents code for the first time to check a folder for new items. Eventual plan is to use the ItemsAdd event to trigger a bunch of other processing, but for now, just trying to save it to a folder and not getting that far.

当我运行下面的Application_Startup代码时,立即窗口显示我找到了正确的clntFldrItms.问题是,如果我随后将一个项目拖到有问题的文件夹中,则不会触发ItemAdd宏.当我尝试为clntFldrItms添加监视时,该变量未设置为任何值.看来,一旦Application_Startup子程序完成,分配就会停止.

When I run the Application_Startup code below, the immediate window shows that I've found the right clntFldrItms. Problem is, if I then drag an item into the folder in question, the ItemAdd macro doesn't fire. When I try to add a watch for clntFldrItms, the variable isn't set to anything. It looks like as soon as the Application_Startup sub finishes, the assignment stops.

所有代码都在ThisOutlookSession对象中.

All code is in the ThisOutlookSession object.

这可能是因为我正在使用SMTP电子邮件地址(例如,而不是Exchange)吗?

Could this be because I'm working with an SMTP email address (rather than Exchange, for example)?

再次感谢您的帮助.

编辑将我的回复添加到尤金的评论中.我注意到,当我打开编辑器并进入Application_Startup子目录时,即使在进入Set clntFldrItms = clntFldr.Items行之前,也已正确分配了clntFldrItms.当我完成所有步骤后,它又消失了.我不能进入ItemAdd子项,但是当我进入其他代码时clntFldrItms没什么.

EDIT Adding my response to Eugene's comment. I noticed that when I open the editor and step into the Application_Startup sub, clntFldrItms is properly assigned, even before I get to the Set clntFldrItms = clntFldr.Items line. As soon as I finish stepping through, it's gone again. I can't step into the ItemAdd sub, but when I step into other code clntFldrItms is Nothing.

最终编辑抱歉,我知道我忘了关闭此功能.我本身无法解决问题,但我意识到这是由于我的SMTP帐户所致.当我在Exchange上尝试使用它时,它起作用了.除非我在Exchange中工作,否则似乎不会触发该事件.

FINAL EDIT Sorry, I realize I forgot to close this off. I wasn't able to solve the problem per se, but I realized it was due to my SMTP account. When I tried it at work with Exchange, it worked. It seems that the event doesn't fire unless I'm working in Exchange.

Option Explicit

Public WithEvents clntFldrItms As Outlook.Items

Private Sub Application_Startup()
    Dim clntFldr As MAPIFolder
    Set clntFldr = Application.Session.GetDefaultFolder(olFolderSentMail).Folders("Client Emails")
    Set clntFldrItms = clntFldr.Items
    Set clntFldr = Nothing
    Debug.Print clntFldrItms.item(1).Subject
End Sub

Private Sub clntFldrItms_ItemAdd(ByVal item As Object)
    Dim bChar As String
    bChar = "\/:*?™""® <>|.&@#_+`©~;-+=^$!,'" & Chr(34)
    Dim saveName As String
    If item.Class = olMail Then
        saveName = item.Subject
        For x = 1 To Len(bChar)
            saveName = Replace(saveName, Mid(bChar, x, 1), "-")
        Next x
        item.SaveAs "C:\Users\User\Google Drive\8 - VBA work\Preparation for Assisted Responder\Sent Messages Folder\" & _
        saveName & ".msg", olMSG
    End If
End Sub

推荐答案

尝试在ItemAdd事件处理程序中设置断点,并在命中断点时在其中检出clntFldrItms对象.

Try to set a breakpoint in the ItemAdd event handler and check out the clntFldrItms object there when the breakpoint is hit.

请注意,同时添加多个项目(超过16个-这是Outlook中的一个众所周知的问题)时,不会触发ItemAdd事件.

Be aware, the ItemAdd event is not fired when multiple items were added at the same time (more than 16 - this is a well-known issue in Outlook).

您可能会找到入门在Outlook 2010中使用VBA 文章可笑.

You may find the Getting Started with VBA in Outlook 2010 article hellpful.

编辑设置clntFldrItms是因为在启动Outlook时运行了启动事件处理程序.因此,该对象将在幕后启动时初始化.

EDIT The clntFldrItms is set because the Startup event handler is run when you start Outlook. So, the object is initialized at startup behind the scene.

这篇关于子完成后,Application_Startup变量消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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