Exporting undelivered emails from Outlook to Excel

Don4000

Board Regular
Joined
Jan 28, 2005
Messages
91
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
 

Don4000

Board Regular
Joined
Jan 28, 2005
Messages
91
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
 

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
441
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:

Don4000

Board Regular
Joined
Jan 28, 2005
Messages
91
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
 

Paul Ked

Active Member
Joined
Jun 4, 2015
Messages
441
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
 

Don4000

Board Regular
Joined
Jan 28, 2005
Messages
91
Hello Paul
Macro appears to run, but still does not produce data in sheet1

Best wishes
Don
 

Forum statistics

Threads
1,078,437
Messages
5,340,274
Members
399,361
Latest member
Linford

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top