1
votes

I am newbie in using VBA I suppose that I want to automate export data from outlook mail and import to excel file xlsx as I searched for a tutorial for this and I find this tutorial https://www.exceltrainingvideos.com/get-data-from-outlook-into-excel-worksheet/ I followed these steps by creating a module as I inserted in this Module this code

Sub GetDataFromOutlook()
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("SSV")

i = 1

    For Each OutlookMail In Folder.Items
    If OutlookMail.ReceivedTime >= Range("Email_Receipt_Date").Value And 
Date <= 30 / apr / 2019 Then

    Range("email_Subject").Offset(i, 0) = 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_Body").Offset(i, 0).Value = OutlookMail.Body
    Range("email_Body").Offset(i, 0).Columns.AutoFit
    Range("email_Body").Offset(i, 0).VerticalAlignment = xlTop
    i = i + 1
End If

Next OutlookMail

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




End Sub

and finally when I am trying to run it I found this error

Run-error '1004' Method 'Range' of object '_Global) failed

It may be the error in excel file I have?

1
Date <= 30 / apr / 2019 won't do what you think.Vincent G
Did you put your code in an Excel module or in a Outlook module? it need to be on Excel side.Vincent G

1 Answers

0
votes

Yes, the error is relative to your Excel sheet. It's reporting that it was unable to find those ranges. You have to create them on your sheet before launching the macro.

To create a custom Range you have to click on your cell and then edit its name, like in this picture:

enter image description here


Hope this helps.

Note: Date <= 30 / apr / 2019 will not work. Change it in Date <= "30/04/2019" and it will work as intended.