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 ?
Let me know in the comments section if you have any questions.
转载于:https://www.cnblogs.com/jjj250/p/11598144.html
- 点赞
- 收藏
- 分享
- 文章举报
- How to backup and restore data and e-mail account settings in Outlook 2007
- How to backup and restore data and e-mail account settings in Outlook 2007
- How To Determine When a Page Is Done Loading in WebBrowser Control
- PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the Eve
- RunningMapReduceExampleTFIDF - hadoop-clusternet - This document describes how to run the TF-IDF MapReduce example against ascii books. - This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (1-10 nodes) - Google Pro
- RunningMapReduceExampleTFIDF - hadoop-clusternet - This document describes how to run the TF-IDF MapReduce example against ascii books. - This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (1-10 nodes) - Google Pro
- How to run a user control assembly that is hosted on Internet Information Services (IIS) in Internet Explorer
- How to grant access to SQL logins on a standby database when the guest user is disabled in SQL Serve
- How to reclaim space in InnoDB when innodb_file_per_table is ON
- How to get a type in C++ when its template argument is the argument
- If you have multiple web application run how to determin which is yours when you debug using visual studio 2008?
- 虚拟主机运行ASP错误解决:HTTP/1.1 New Application Failed when allowSessionState is set to false in web.config
- 《How to run one script by command line in 3dsMax》
- Intel HAXM is required to run this AVD. /dev/kvm is not found.Enable vt-x in bios security settings.
- linux, how to define a new array in bash
- How to make JDev run ant script in debug mode permanently
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC (文档 ID 28
- How to determine who is logged on to a database by using Microsoft Jet UserRoster in Access 2000
- How To Create a New User and Grant Permissions in MySQL
- How to fix "'user' is not in sudoers" in CentOS