在Outlook中收到新邮件后,如何触发宏运行? [英] How do I trigger a macro to run after a new mail is received in Outlook?

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

问题描述

我正在编写一个宏,该宏基于Nagios服务器作为电子邮件收到的警报在数据库上创建票证.但是,在检查邮件时,我不能让宏以无限循环的方式运行,因为它占用的资源太多,会使我的桌面挂起.我需要找到一种仅在收到新邮件时才触发宏的方法.

I'm writing a macro that creates tickets on a database based on alerts received from a Nagios server as an email. However, I cannot let the macro run in an infinite loop while checking for mails because it is just too resource heavy and makes my desktop hang. I need to find a way to trigger the macro only when a new mail is received.

我在MSDN网站上寻找与NewMail事件类似的东西,但是找不到任何连贯的东西.谁能向我展示一些示例代码来说明如何从新邮件事件中触发宏?

I looked for something along the lines of NewMail events on the MSDN website, but I can't find anything coherent. Can anyone show me just a bit of sample code to show how to trigger macros from new mail events?

推荐答案

此代码会将事件侦听器添加到默认的本地收件箱,然后对收到的电子邮件采取一些措施.您需要在下面的代码中添加该操作.

This code will add an event listener to the default local Inbox, then take some action on incoming emails. You need to add that action in the code below.

Private WithEvents Items As Outlook.Items 
Private Sub Application_Startup() 
  Dim olApp As Outlook.Application 
  Dim objNS As Outlook.NameSpace 
  Set olApp = Outlook.Application 
  Set objNS = olApp.GetNamespace("MAPI") 
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items 
End Sub
Private Sub Items_ItemAdd(ByVal item As Object) 

  On Error Goto ErrorHandler 
  Dim Msg As Outlook.MailItem 
  If TypeName(item) = "MailItem" Then
    Set Msg = item 
    ' ******************
    ' do something here
    ' ******************
  End If
ProgramExit: 
  Exit Sub
ErrorHandler: 
  MsgBox Err.Number & " - " & Err.Description 
  Resume ProgramExit 
End Sub

将代码粘贴到ThisOutlookSession模块中之后,必须重新启动Outlook.

After pasting the code in ThisOutlookSession module, you must restart Outlook.

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

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