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版本需求 ...
随机推荐
- Tensorflow模型代码调试问题
背景: 不知道大家有没有这样的烦恼:在使用Tensorflow搭建好模型调试的过程中,经常会碰到一些问题,当时花了不少时间把这个问题解决了,一段时间后,又出现了同样的问题,却怎么也不记得之前是怎么解决 ...
- Ubuntu 16.04 装机后如何永久更改ulimit和修改MySQL的存储路径datadir
Ubuntu 16.04 装机后的配置要点: 1. 网络的配置 2. 更改源列表 3. 永久更改ulimit ulimit限制着程序打开文件的数目,默认情况下为1024,作为服务器使用时,这个数字往往 ...
- IPC之util.c源码解读
// SPDX-License-Identifier: GPL-2.0 /* * linux/ipc/util.c * Copyright (C) 1992 Krishna Balasubramani ...
- Hive-ha (十三)
hive-high Avaliable hive的搭建方式有三种,分别是 1.Local/Embedded Metastore Database (Derby) 2.Remote Meta ...
- C - Nuske vs Phantom Thnook
题意:n*m矩阵,n,m<=2e3,矩阵中的1能走到相邻4个1上,0代表障碍,若两个1联通 则只有一条路径 q个询问,q<=2e5,每次询问一个子矩阵中有多少个连通分量? 同一个连通分量中 ...
- 转 shell中的多进程【并发】
原文地址https://bbs.51cto.com/thread-1104907-1-1.html 根据我个人的理解, 所谓的多进程 只不过是将多个任务放到后台执行而已,很多人都用到过,所以现在讲的主 ...
- Ubuntu18.04安装破解版MATLAB2018b
参考链接: Ubuntu 18.04安装破解Matlab 2018b及创建快捷方式的详细教程(附图) Linux下安装JDK遇到的问题之cp: 无法获取"jdk-8u191-linux-i5 ...
- Python标准库: turtle--海龟绘图。
turtle --- 海龟绘图 (点击查看官方文档.) 简介 import turtle # 调用该库.Turtle的实例对象,默认命名为turtle. turtle.forward(10) from ...
- js创建对象的几种方式(工厂模式、构造函数模式、原型模式)
普通方法创建对象 var obj = { name:"猪八戒", sayname:function () { alert(this.name); } } var obj1 = { ...
- Servlet中的乱码问题及解决办法
假设现在有个form表单,当页面中提交一个包含中文的请求时,在服务端有可能出现中文乱码问题. <!DOCTYPE html> <html> <head> <m ...