Get Email Data from Outlook to Excel

Akshay_divecha

Board Regular
Joined
Mar 11, 2014
Messages
70
Hello Experts,

Working to find a VBA code which can copy details from outlook to excel and below is the code but while run the code getting below error.

Pls help to advise what's wrong,

Screenshot_1.png



VBA Code:
Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutApp = CreateObject("outlook.application")
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders(“inbox”)

i = 1

For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range(“Email_Receipt_Date”).Value And Date <= 1 - Feb - 2020 Then
Range("email_Subject").Offset(i, 0).Value = OutlookMail.Subject
Range(“email_Subject”).Offset(i, 0).Columns.AutoFit
Range(“email_Subject”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Date”).Offset(i, 0).Value = OutlookMail.ReceivedTime
Range(“email_Date”).Offset(i, 0).Columns.AutoFit
Range(“email_Date”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_Sender”).Offset(i, 0).Value = OutlookMail.SenderName
Range(“email_Sender”).Offset(i, 0).Columns.AutoFit
Range(“email_Sender”).Offset(i, 0).VerticalAlignment = xlTop
Range(“email_CC”).Offset(i, 0).Value = OutlookMail.Body
Range(“email_CC”).Offset(i, 0).Columns.AutoFit
Range(“email_CC”).Offset(i, 0).VerticalAlignment = xlTop

i = i + 1
End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Excel doesn't know anything about Outlook unless you tell it where to look.

On the VBA toolbar click on Tools and References.
Tick the Microsoft Outlook xx.x Object Library.
 
Upvote 0
Ah, you got me there then.

Not sure.
You have defined the OutlookApp variable, but then used OutApp though which would throw a Variable not defined error if you have Option Explicit on, or an Object variable or With block variable not set error if you didn't. - that wouldn't cause the error you're getting though.
 
Upvote 0
Ah, you got me there then.

Not sure.
You have defined the OutlookApp variable, but then used OutApp though which would throw a Variable not defined error if you have Option Explicit on, or an Object variable or With block variable not set error if you didn't. - that wouldn't cause the error you're getting though.

Changed as below, but still there's an error..

Could you help me to fix it please..

VBA Code:
Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders(“Inbox”)
 
Upvote 0
Ah, you got me there then.

Not sure.
You have defined the OutlookApp variable, but then used OutApp though which would throw a Variable not defined error if you have Option Explicit on, or an Object variable or With block variable not set error if you didn't. - that wouldn't cause the error you're getting though.

Hey... can you help me to fix the problem please...
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,614
Members
449,039
Latest member
Mbone Mathonsi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top