VBA Code to Read Outlook Emails
Read Outlook Emails Code:
Public Sub ReadOutlookEmails() 'Microsoft Outlook XX.X Object Library is required to run this code 'Variable declaration Dim objFolder As Outlook.Folder Dim objNS As Outlook.Namespace Dim objMail As Outlook.MailItem Dim lCounter As Long 'Set objects Set objNS = Outlook.GetNamespace("MAPI") Set objFolder = objNS.PickFolder 'Validation if no folder picked by the user If TypeName(objFolder) = "Nothing" Then Exit Sub End If 'Read emails from Outlook folder and update details in Sheet1 For lCounter = 1 To objFolder.Items.Count Set objMail = objFolder.Items.Item(lCounter) Sheet1.Range("A" & lCounter + 5).Value = objMail.SenderName 'Sender name Sheet1.Range("B" & lCounter + 5).Value = objMail.To 'To Sheet1.Range("C" & lCounter + 5).Value = objMail.CC 'Cc Sheet1.Range("D" & lCounter + 5).Value = objMail.Subject 'Subject Sheet1.Range("E" & lCounter + 5).Value = objMail.ReceivedTime 'Email Received Time Sheet1.Range("F" & lCounter + 5).Value = objMail.Attachments.Count 'Attachment Count Next 'Show confirmation message to user MsgBox "Done", vbInformation End Sub
It is worth to mention that you must have MS Outlook installed in your system to use this code and you also need to add Office reference (Microsoft Outlook XX.X Object Library) in Excel VBA from Menu Bar (Tools>References…). Below are the steps to add Office reference in Excel VBA:
1. From the Menu Bar, click on Tools > References…
To use this code in your Excel file, follow below steps:
1. Open an Excel file
2. Press Alt+F11
3. Insert a Module (Insert>Module) from menu bar
4. Paste the code in the module
5. Now add a shape in Excel sheet
6. Give a name to the shape like ‘Read Outlook Emails’
7. Right click on the shape and select ‘Assign Macro…’
8. Select ‘ReadOutlookEmails’ from the list and click on ‘Ok’ button
Download Practice File
Excel VBA Course : Beginners to Advanced
We are currently offering our Excel VBA Course at discounted prices. This courses includes On Demand Videos, Practice Assignments, Q&A Support from our Experts. Also after successfully completion of the certification, will share the success with Certificate of Completion
This course is going to help you to excel your skills in Excel VBA with our real time case studies.
Lets get connected and start learning now. Click here to Enroll.
Secrets of Excel Data Visualization: Beginners to Advanced Course
Here is another best rated Excel Charts and Graph Course from excelsirji. This courses also includes On Demand Videos, Practice Assignments, Q&A Support from our Experts.
This Course will enable you to become Excel Data Visualization Expert as it consists many charts preparation method which you will not find over the internet.
So Enroll now to become expert in Excel Data Visualization. Click here to Enroll.
Use Automated Excel Utility Tools to increase productivity
Hope you are enjoying learning Excel with us, if you want any support related to this article, please do comment else you can ask questions in Excel Community
Excellent!!! a few minor modifications, and it worked perfectly!
Is Outlook that come with Windows 10 applicable? or is Microsoft Outlook required for VBA? Thanks!
You should have installed and configured Outlook available where you are running the VBA Code.
Can you do this to where you use a specified folder in the code instead of choosing each time?
Yes Rebecca, it is possible to specify the folder in the code. I suggest you to have a look at Outlook Email Management Tool. Once downloaded, we can customize your tool copy for free with remember feature, means the tool will remember last browsed folder to avoid browsing the folder again and again.