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

查看:67
本文介绍了在 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天全站免登陆