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版本需求 ...
随机推荐
- odoo 关系字段(关联关系)
Many-to-one关联 publisher_id = fields.Many2one(comodel_name= 'res.partner', domain='',context={},ondel ...
- 0、CentOS7系统root密码丢失找回方法
第一步 1.重新启动或开启CentOS7.6系统,在选择进入系统的Grub菜单界面如下图1-1,根据提示按"e"小写字母进入编辑界面,如下图1-2所示: 图1-1 CentOS7. ...
- 1.Shell脚本
1.Shell脚本 可以将Shell终端解释器当作人与计算机硬件之间的“翻译官”,它作为用户与Linux系统内部的通信媒介,除了能够支持各种变量与参数外,还提供了诸如循环.分支等高级编 程语言才有的控 ...
- 使用cJSON解析JSON
cJSON获取数组元素的每个值 { "operType": 0x5, "field": ["time","matchRule&qu ...
- 【2017中国大学生程序设计竞赛-哈尔滨站】B - K-th Number
原题: 题意: 给你一个长度为N的正整数组A,对于这个数组的所有子区间,若长度小于k则不管它,若长度大于等于k则取第k大放入数组B 问你B中第M大的数是谁 一眼序列分治,然而没思路 数据结构?能想到从 ...
- 使用CreateWindowEx创建子窗口的注意事项
比如: 使用 HWND child = CreateWindowEx(0,L"childclass",NULL,WS_CHILD | WS_VISIBLE | WS_CLIPSIB ...
- Acwing-167-木棒(搜索, 剪枝)
链接: https://www.acwing.com/problem/content/169/ 题意: 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位. 然后他又 ...
- 51nod 1172 Partial Sums V2
题目 给出一个数组A,经过一次处理,生成一个数组S,数组S中的每个值相当于数组A的累加,比如:A = {1 3 5 6} => S = {1 4 9 15}.如果对生成的数组S再进行一次累加操作 ...
- css-滚动条常用的样式修改
// ::-webkit-scrollbar { /*滚动条整体样式*/// width: 10px; /*宽分别对应竖滚动条的尺寸*/// height: 5px; /*高分别对应横滚动条的尺寸*/ ...
- Open_Read_Write函数基本使用
先来一个小插曲,我们知道read函数等是系统调用函数,应该在第二页的手册里头,可是我man 2 read的时候却找不到,由此到/usr/sharead/man/man2目录下查看的时候发现此目录为空, ...