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…
2. Select ‘Microsoft Outlook XX.X Object Library’ and click on ‘OK’ button
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
9. Done
Have you ever felt that Microsoft should have added a formula in Excel which can count the cells based on specific color? I have seen many code requests to share a VBA code that can count the cells by its color. To help our subscribers and developers, we are sharing 2 codes that be used to count the cells with specific color and returns the count of the matching color cells.
Learn how to write your first VBA macro in Excel to automate repetitive formatting tasks across multiple worksheets. This beginner-friendly guide will walk you through the process step-by-step, from recording your actions to customizing the code for your specific needs.
VBA to Browse Outlook Folder Outlook is most commonly used emailing application used in the world. Many people spend their entire day on Outlook applications to read and respond to emails. To automate certain rule-based…
Free File Renamer Tool – Quickly Rename files batch using Excel VBA Here is another help code and tool for programmers to rename files. You can use this tool for renaming all files available in…
Colorindex in Excel VBA Today let’s try to understand how ColorIndex property in Excel VBA works. It is an easy and effective way to quickly complete the development. ColorIndex property is normally used by VBA…
VBA Code to check if folder exist Validation is one of the important parts of any programming language. As per few studies, 60% of the code is focused on validating input or output. In this…
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.
Regards,
ExcelSirJi Team