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. Use pkgsrc on ARM

    What is this page? This page describes how to use pkgsrc on ARM architecture with EABI support. I bo ...

  2. VM虚拟机下centos安装。

    centOS 7安装步骤: 1.选择新建虚拟机,稍后安装,linux选centos7 64位 2.位置改到存放虚拟机的文件夹 3.把硬盘空间改到40g,内存分到4g,1处理器2个核心 4 更改cd/d ...

  3. python常用模块:pickle、shelve、json、xml、configparser

    今日内容主要有: 一.pickle模块二.shelve模块三.json模块四.json练习五.xml模块 六.xml练习七.configparser模块 一.pickle模块 #pickle是一个用来 ...

  4. Git学习笔记(1)

    一.Git特点 1.直接快照,而非比较差异 Git只关心文件数据的整体是否发生变化,而不关心具体文件及其内容发生了那些变化.也就是说Git在保存文件时,每次只会在上一次版本基础上保存那些变化的文件,为 ...

  5. JMeter函数整理

    "_intSum” 功能:用于计算多个整数的和,可以是计算正整数和负整数的和,它有N个参数,最少有3个参数,最多不限.最后一个参数是函数名称,前面的其它参数是要求和的整数.这个函数在函数对话 ...

  6. Linq表达树(固定参数)

    这篇博客只能用来批判因为我刚刚学习linq对它了解只有简单的linq to sql 的语句所以来写这个博客只能说是班门弄斧了,看的下去的话就坚持看吧. 在网上看了别人的文章目前水平有限借鉴别人的思想吧 ...

  7. POJ1639 算法竞赛进阶指南 野餐规划

    题目描述 原题链接 一群小丑演员,以其出色的柔术表演,可以无限量的钻进同一辆汽车中,而闻名世界. 现在他们想要去公园玩耍,但是他们的经费非常紧缺. 他们将乘车前往公园,为了减少花费,他们决定选择一种合 ...

  8. Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)

    Pursuit For Artifacts CodeForces - 652E Johnny is playing a well-known computer game. The game are i ...

  9. springboot2.1.7整合Druid

    一.maven的依赖:文中就贴重点的, 其他依赖就不贴了 <dependency> <groupId>com.alibaba</groupId> <artif ...

  10. 修改Jenkins目录

    注意:在Jenkins运行时是不能更改的. 请先将Jenkins停止运行. 1.windows环境下更改JENKINS的主目录 Windows环境中,Jenkins主目录默认在C:\Documents ...