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
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 CODE TO HIDE MENU RIBBON IN MS ACCESS In MS Access, there are multiple ways to protect your code or tool from un-authorized access and edits. Some developers prefers to hide MS Access ribbons…
Excel Files and Sheets Consolidator is an MS Excel based data consolidation tool which can be used to consolidate data from multiple Excel Files or Excel Sheets. The tool supports multiple configurations such as Sheet Name, Sheet Index, Header Row and Non-Blank column to help consolidating accurate data.
File Manager tool is an Excel based tool which helps you to delete or move unwanted files from your system. It requires a source and destination folder (in case you want to move files). First it lists all the files available in the folder or sub-folders then you can select the action to be taken for each file such as Move or Delete. With a click of button, tool will take all necessary actions.
Table of Content How does testing help? How should I test my tool or application? How does Dummy Data Generator tool helps in testing? How do I use this tool? From many years I have…
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…
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