How to run a VBA macro when new mail is received in Outlook
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:
- Option Explicit
- Private WithEvents inboxItems As Outlook.Items
- Private Sub Application_Startup()
- Dim outlookApp As Outlook.Application
- Dim objectNS As Outlook.NameSpace
- Set outlookApp = Outlook.Application
- Set objectNS = outlookApp.GetNamespace("MAPI")
- Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
- End Sub
- Private Sub inboxItems_ItemAdd(ByVal Item As Object)
- On Error GoTo ErrorHandler
- Dim Msg As Outlook.MailItem
- Dim MessageInfo
- Dim Result
- If TypeName(Item) = "MailItem" Then
- MessageInfo = "" & _
- "Sender : " & Item.SenderEmailAddress & vbCrLf & _
- "Sent : " & Item.SentOn & vbCrLf & _
- "Received : " & Item.ReceivedTime & vbCrLf & _
- "Subject : " & Item.Subject & vbCrLf & _
- "Size : " & Item.Size & vbCrLf & _
- "Message Body : " & vbCrLf & Item.Body
- Result = MsgBox(MessageInfo, vbOKOnly, "New Message Received")
- End If
- ExitNewItem:
- Exit Sub
- ErrorHandler:
- MsgBox Err.Number & " - " & Err.Description
- Resume ExitNewItem
- 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的更多相关文章
- [转]Introduction - Run Excel Macro using VBScript
本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...
- Excel VBA to Interact with Other Applications
转载自:https://analysistabs.com/excel-vba/interact-with-other-applications/ Interact with PowerPoint fr ...
- Excel VBA发送Email时自动允许Outlook安全对话框
在Outlook的宏安全性设置如果选择了“为所有宏提供通知” 并且,在[编程访问]中选择了“总是向我发出警告” 在其他VBA中创建邮件过程中,如果修改Recipients或者执行Send方法,都会弹出 ...
- arcmap Command
The information in this document is useful if you are trying to programmatically find a built-in com ...
- journal
dec 5 rpt prep exam dec 4 lie to me dec 3 exam dec 2 preparation for exam dec 1 preparation for exam ...
- xlrd doc
The xlrd Module A Python module for extracting data from MS Excel ™ spreadsheet files. Version 0.7.3 ...
- Excel 2007 若干技巧。
1.自定义序列 office按钮→excel选项→常用→编辑自定义列表 2.无法清空剪贴板错误的处理办法: 取消"显示粘贴选项"选项 3.每次选定同一单元格 输入后按ctrl+En ...
- excel 添加换行符,去除换行符:
excel 中添加换行符: :alt+enter 去掉excel中的换行符有三种方法: 注:解决过程中翻阅其他博客,看到如下方式: 1.看到有的说全选后“取消自动换行”,保存后,再打开,依然存在换行符 ...
- OLE工具套件分析OFFICE宏恶意样本
零.绪论:OLE工具套件的介绍 OLE工具套件是一款针对OFFICE文档开发的具有强大分析功能一组工具集.这里主要介绍基于Python2.7的OLEtools的安装和使用. (1)Python版本需求 ...
随机推荐
- Linux工具之watch
watch watch 监测一个命令的运行结果 -n 指定间隔的时间 -d watch会高亮显示变化的区域. -t 会关闭watch命令在顶部的时间间隔, ...
- python常用模块:项目目录规范、定制程序入口、自定义模块、引用变量、time和datetime模块
今天讲课内容有两大部分: 一.文件目录规范二.定制程序入口三.使用标准目录后四.常规函数time.datetime 一.标准目录规范 之前用过的案例atm机+购物商城代码过长,在学习了模块和包以后,我 ...
- AspectJ的表达式实例
Joinpoints 连接点,通俗的讲就是想要横切的目标,这些目标包括方法(Method),构造器(Constructor),域(Field),异常(Exception),对象和类初始化(Object ...
- 【BZOJ 3514】Codechef MARCH14 GERALD07 加强版
题意 \(n\) 个点 \(m\) 条边的无向图,\(k\) 次询问保留图中编号在 \([l,r]\) 的边的时候图中的联通块个数.强制在线. \(n,m,k\le 2\times 10^5\) 题解 ...
- eclipse+springboot+tomcat自带的部署
最近在看微服务,然后整理了两个springboot.但执行都是内部main执行,想着后期应该会用到tomcat,大部分都是说的打成war包,然后部署到tomcat上. war包的方式就不说了,网上很多 ...
- php类知识----特别用法
spl_autoload_register注册 <?php #spl_autoload_register-----这个例子是用来打印实例化类的类名 function thereisagameof ...
- Python之PyLint自动检查代码
PyLint的下载地址:https://pypi.python.org/pypi/pylint PyLint的官网:http://www.pylint.org/ 从源码发行版安装,解压文件包并且运行 ...
- @Valid与@Validated
Spring Validation验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范,是标准JSR-303的一个变种),javax提供了@Valid(标准JSR- ...
- Python SMTP发送邮件Ⅱ
使用Python发送HTML格式的邮件 Python发送HTML格式的邮件与https://www.xuanhe.net/weixiu/4271.html发送纯文本消息的邮件不同之处就是将MIMETe ...
- sqoop参数详解
从RDBMS到HIVE: sqoop import --connect jdbc:oracle:thin:@//192.168.156.111/test--username test --passwo ...