It can be very useful to run a VBA macro when new mail is received in Outlook. A customer asked me to write something that would log an entry to a SQL database when an email produced contact form was received.

It’s easy to do but can take a bit of trial and error to get working just how you want it.

You need to add an event listener to the Inbox which will process incoming messages. A the following code to ThisOutlookSession:

  1. Option Explicit
  2. Private WithEvents inboxItems As Outlook.Items
  3. Private Sub Application_Startup()
  4. Dim outlookApp As Outlook.Application
  5. Dim objectNS As Outlook.NameSpace
  6. Set outlookApp = Outlook.Application
  7. Set objectNS = outlookApp.GetNamespace("MAPI")
  8. Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
  9. End Sub
  10. Private Sub inboxItems_ItemAdd(ByVal Item As Object)
  11. On Error GoTo ErrorHandler
  12. Dim Msg As Outlook.MailItem
  13. Dim MessageInfo
  14. Dim Result
  15. If TypeName(Item) = "MailItem" Then
  16. MessageInfo = "" & _
  17. "Sender : " & Item.SenderEmailAddress & vbCrLf & _
  18. "Sent : " & Item.SentOn & vbCrLf & _
  19. "Received : " & Item.ReceivedTime & vbCrLf & _
  20. "Subject : " & Item.Subject & vbCrLf & _
  21. "Size : " & Item.Size & vbCrLf & _
  22. "Message Body : " & vbCrLf & Item.Body
  23. Result = MsgBox(MessageInfo, vbOKOnly, "New Message Received")
  24. End If
  25. ExitNewItem:
  26. Exit Sub
  27. ErrorHandler:
  28. MsgBox Err.Number & " - " & Err.Description
  29. Resume ExitNewItem
  30. End Sub

You need to restart Outlook for the code to become active.

The above code will produce a simple message box that shows some of the message properties:

You can of course do whatever you like with the message when it is received. I used it to insert rows into a SQL table, then move the message to a different folder. It works very well.

It’s worth taking a look at all of the available properties of the Outlook mailitem that are available.

If you found this post helpful, I’d really appreciate it if you would rate it

How to run a VBA macro when new mail is received in Outlook的更多相关文章

  1. [转]Introduction - Run Excel Macro using VBScript

    本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...

  2. Excel VBA to Interact with Other Applications

    转载自:https://analysistabs.com/excel-vba/interact-with-other-applications/ Interact with PowerPoint fr ...

  3. Excel VBA发送Email时自动允许Outlook安全对话框

    在Outlook的宏安全性设置如果选择了“为所有宏提供通知” 并且,在[编程访问]中选择了“总是向我发出警告” 在其他VBA中创建邮件过程中,如果修改Recipients或者执行Send方法,都会弹出 ...

  4. arcmap Command

    The information in this document is useful if you are trying to programmatically find a built-in com ...

  5. journal

    dec 5 rpt prep exam dec 4 lie to me dec 3 exam dec 2 preparation for exam dec 1 preparation for exam ...

  6. xlrd doc

    The xlrd Module A Python module for extracting data from MS Excel ™ spreadsheet files. Version 0.7.3 ...

  7. Excel 2007 若干技巧。

    1.自定义序列 office按钮→excel选项→常用→编辑自定义列表 2.无法清空剪贴板错误的处理办法: 取消"显示粘贴选项"选项 3.每次选定同一单元格 输入后按ctrl+En ...

  8. excel 添加换行符,去除换行符:

    excel 中添加换行符: :alt+enter 去掉excel中的换行符有三种方法: 注:解决过程中翻阅其他博客,看到如下方式: 1.看到有的说全选后“取消自动换行”,保存后,再打开,依然存在换行符 ...

  9. OLE工具套件分析OFFICE宏恶意样本

    零.绪论:OLE工具套件的介绍 OLE工具套件是一款针对OFFICE文档开发的具有强大分析功能一组工具集.这里主要介绍基于Python2.7的OLEtools的安装和使用. (1)Python版本需求 ...

随机推荐

  1. 8.2.ZooKeeper应用场景

    7.ZooKeeper应用举例 为了方便大家理解ZooKeeper,在此就给大家举个例子,看看ZooKeeper是如何实现的他的服务的,我以ZooKeeper提供的基本服务分布式锁为例. 7.1 分布 ...

  2. 【ios bug解决】 输入框聚焦时光标不显示

    解决办法:重写user-select样式 css:   user-select: text;-webkit-user-select:text;

  3. elk快速入门-Logstash

    Logstash1.功能:数据输入,数据筛选,数据输出2.特性:数据来源中立性,支持众多数据源:如文件log file,指标,网站服务日志,关系型数据库,redis,mq等产生的数据3.beats:分 ...

  4. android 拍照上传文件 原生定位

    最近公司需要一个android拍照上传和定位功能的的单一功能页面,一开始选择ionic cordova angular的一套H5框架,但是遇到和上传文件报错的问题,bug找了一天没找到原因,怀疑是io ...

  5. myBatis整合Spring理论

    整合目标 更少的编码 更少的配置 足够的灵活性 1.更少的编码 只写接口,不写实现(myBatis帮我们实现接口) 2-1.更少的配置-别名 在java的规范当中,表示一个类型是通过包名+类名,但是整 ...

  6. Tomcat基础知识

    介绍Tomcat之前先介绍下Java相关的知识. 各常见组件: 1.服务器(server):Tomcat的一个实例,通常一个JVM只能包含一个Tomcat实例:因此,一台物理服务器上可以在启动多个JV ...

  7. HNOI2004 树的计数 和 HNOI2008 明明的烦恼

    树的计数 输入文件第一行是一个正整数n,表示树有n个结点.第二行有n个数,第i个数表示di,即树的第i个结点的度数.其中1<=n<=150,输入数据保证满足条件的树不超过10^17个. 明 ...

  8. Java 转发和重定向的区别

    转发是服务器行为,重定向是客户端行为 1.转发在服务器端完成的;重定向是在客户端完成的 2.转发的速度快;重定向速度慢 3.转发的是同一次请求;重定向是两次不同请求 4.转发不会执行转发后的代码;重定 ...

  9. js数据持久化本地数据存储-JSON.parse和JSON.stringify的区别

    JSON.stringify()的作用是将 JavaScript 值转换为 JSON 字符串, 而JSON.parse()可以将JSON字符串转为一个对象. 简单点说,它们的作用是相对的,我用JSON ...

  10. VC:检测网络连接的方法

    方法一: #include "stdafx.h" #include "windows.h" #include <Sensapi.h> #includ ...