Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to import emails from a specific folder in Outlook into excel using VBA.

Getting the following error at this line:
runtime error 1004 Method range of object _global failed.
Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject

Assuming it it a invalid data type or something. Also assuming it will happen to all ranges below.


I am using the following code from this source:
How To Import Your Outlook Emails Into Excel With VBA | How To Excel[^]
Sub test()

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("veeam")

i = 1

For Each OutlookMail In Folder.Items
    If OutlookMail.ReceivedTime >= Range("B1").Value Then
        Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
        Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
        Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
        Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body
        
        i = i + 1
    End If
Next OutlookMail

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


End Sub


What I have tried:

Found a few resources only. Something having to do with Text instead of a number and also reverse but seems to become worse.
Posted
Updated 13-Oct-20 7:43am
v2
Comments
[no name] 13-Oct-20 13:53pm    
All this depends on how you formatted your Excel sheet (which no one else can see), according to the article.
Richard MacCutchan 14-Oct-20 4:27am    
Use the debugger in the macro to see what values it is using in the Range statement and why they may be wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900