触发VBA代码在Outlook中收到新邮件后运行吗? [英] Trigger VBA code to run after a new mail is received in Outlook?

查看:504
本文介绍了触发VBA代码在Outlook中收到新邮件后运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Win 7,Outlook 2013,我使用VBA代码对收件箱中到达的某些文件执行操作.但是,我必须单击/运行按钮才能运行此宏.

Win 7, Outlook 2013 I use VBA code that takes an action on some of the files that arrive in my inbox. However, I have to click/run button to run this macro.

有没有一种方法可以在电子邮件到达时自动运行此代码?

Is there a way that this code could run automatically when an email arrives?

我尝试使用Outlook规则运行脚本,但未成功.

I have tried an Outlook rule to run the script but not successful.

我尝试过此操作,但这仅在运行宏

I tried this, but this works only when once I run the macro

Private Sub Application_NewMail()
    Call GetAttachments_From_Inbox (My Macro)
End Sub

推荐答案

我专门使用规则运行的脚本,该脚本适用于所有消息.这使您可以轻松,清晰地访问收到的邮件.由于您希望在每封邮件中运行此消息,因此最好在收件箱中设置WithEvents .

I specifically use a script I run as a rule, applied to all messages. This gives you easy and clear access to the mail item you receive. Since you want to run this on each message setting up WithEvents on your inbox is probably your best bet.

例如:

您可以如下创建Outlook文件夹的侦听器:

You can create a listeners for an Outlook folder as follows:

Private WithEvents mainInboxItems As Outlook.Items

Public Sub Application_Startup()

    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")

    Set mainInboxItems = objNS.Folders("whatever your main mailbox is called").Folders("AssignNumber").Items
    'assumes your "AssignNumber" folder is a subfolder of the main inbox
    'otherwise you can nest Folders("myArchive").Folders("AssignNumber).items etc
End Sub

您也可以根据需要对任意数量的文件夹执行此操作.

You can do this for as many folders as you want, too.

然后可以将ItemAdd方法分配给每个方法,例如:

You can then assign the ItemAdd method to each of them like:

Private Sub mainInboxItems_ItemAdd(ByVal item As Object)
'do Stuff to mailitem
 Call GetAttachments_From_Inbox (item)
End Sub

所有这些代码都可以放入ThisOutlookSession中.

All this code can go in ThisOutlookSession.

这篇关于触发VBA代码在Outlook中收到新邮件后运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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