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
VBA Code to Filter Data in Excel Here is an easy reference code which filters data in the sheet. In the code, we have filtered the data in three steps. Step 1: Remove existing filter from…
This Excel VBA Code helps to Get User Name. Here is an example environ(username) or Application.username.This macro gets the username from active directory.
How to Export Access Data to Excel using VBA Code? Creating a VBA tool in MS Access is always better as compare to MS Excel. MS Access provides better user interface and ability to handle…
Random Rows Selector is an MS Excel based tool which can be used to pick random or stratified samples from a set of records available in the Excel. The tool is fully dynamic, it can support any data format in Excel.
What is the Usage of sheet color in Excel? When we prepare a report or a dashboard it is easy to identify or analyze reports with a change of color sheet tabs. Analysts generally give…
Excel VBA Tool To Get File Properties Here is one more interesting VBA tool from the ExcelSirJi team. File Properties Tool is an Excel VBA tool that gets the following properties of the file. File…
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