Exporting undelivered emails from Outlook to Excel

Don4000

Board Regular
Joined
Jan 28, 2005
Messages
93
As the title suggest, I'm trying to export Undelivered emails in Outlook to Excel (sheet1)
I found this code on the web, and I'm having a little difficulty getting it to work
Ideally I would like the data to appear in columns, a separate column for Email address etc
The Emails are in sub-folder of the Inbox called undelivered.Its running to close to the end and stopping at
Next OutlookMail

If someone could look at it, I would appreciate it very much
Thank you for your help



Sub GetFromOutlook4()

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("undelivered")
'.Folders("Sales")

i = 1

For Each OutlookMail In Folder.Items
If TypeName(OutlookMail) = "MailItem" Then
If CDate(OutlookMail.ReceivedTime) >= Range("01_01_2019").Value Then
If CDate(OutlookMail.ReceivedTime) <= Range("12_11_2019").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



Best wishes
Don
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi Paul
Thank you for your interest and reply
I tries Next, and it did not work
Still stopping at Next

Any suggestions?

Thanks Don
 
Upvote 0
It's always easier to read code if you indent properly. There were some iffy "If" statements and I've corrected the code so that it checks between the two dates and, if it is, then paste the data...

Code:
Sub GetFromOutlook4()


    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("undelivered")
'.Folders("Sales")


    i = 1


    For Each OutlookMail In Folder.Items
        If TypeName(OutlookMail) = "MailItem" Then
            If CDate(OutlookMail.ReceivedTime) >= Range("01_01_2019").Value _
                And CDate(OutlookMail.ReceivedTime) <= Range("12_11_2019").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
            End If
            i = i + 1
        End If
    Next
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing
    
End Sub

I note that you've posted 89 times and you are still not using CODE tags when posting code. Please do so in future as it makes it much easier for anyone trying to help you. I am, like some others, reluctant to help people who don't follow the forum rules ;)
 
Last edited:
Upvote 0
Paul

Fortunately its been a few years since I was last here
I will of course follow the rules of the forum in future, my apologies for not doing so
I have tried your code, t does appear to run, but produces no data

Any help you can give is appreciated
Best wished
Don
 
Upvote 0
Code:
Sub GetFromOutlook4()
    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("undelivered")
    i = 1
    For Each OutlookMail In Folder.Items
        If TypeName(OutlookMail) = "MailItem" Then
            Range("G" & i) = OutlookMail.Subject
            Range("H" & i) = OutlookMail.ReceivedTime
            Range("I" & i) = OutlookMail.SenderName
            Range("J" & i) = OutlookMail.Body
            i = i + 1
        End If
    Next
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing
End Sub

This will put the data in G1:J1 down
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,692
Members
452,994
Latest member
Janick

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