Search code examples
vbaemailoutlookexchange-server

Reprocessing Outlook Undelivered Mail


I have an Exchange mailbox with a bunch of Outlook ReportItem Undelivered messages. I am attempting to reprocess the undelivered messages via an Outlook VBA script by invoking the "SendAgain" operation on the ReportItem messages. My issue is that the ReportItem does not have a send method, so I have no way of actually sending the reprocessed messages. I am using the following code to go through the messages:

Dim objApp As Outlook.Application
Dim objNameSpace As NameSpace
Dim journalAlertInbox As Folder

Dim objInspector As Inspector
Dim resendItem As ReportItem

Set objApp = CreateObject("Outlook.Application")
Set objNameSpace = objApp.GetNamespace("MAPI")
Set journalAlertInbox = objNameSpace.Stores.Item("thestore").GetDefaultFolder(olFolderInbox)

For Each folderItem In journalAlertInbox.Items
    If TypeOf folderItem Is ReportItem Then
        folderItem.Display
        Set objInspector = folderItem.GetInspector
        objInspector.CommandBars.ExecuteMso "SendAgain"
    
        Set resendItem = Application.ActiveInspector.CurrentItem
        Set objInspector = resendItem.GetInspector
        ''how do I send the item that is now displayed?
        ''resendItem.Close olSave
        folderItem.Close olDiscard
    End If
Next folderItem

I thought I might be able to save the displayed message as a draft, however If I uncomment the resendItem.close olSave line this results in a message in my Outlook Drafts folder of type ReportItem. I can open up the saved draft message it the Outlook GUI and click the send button, but I do not see a way to actually invoke the send operation programmatically. Examining the message in drafts shows it to be of type ReportItem, which does not have a .Send method.

How can I invoke the "Send" operation on the Report Item? I can clearly see the "Send" button, but there seems to be no programmatic way of actually clicking it.

enter image description here


Solution

  • OOM does not expose any functionality that allows to link a ReportItem object to the original MailItem, and, generally, there might not be any kind of link between the two. The best you can do is to retrieve PR_ORIGINAL_SEARCH_KEY MAPI property (or PR_REPORT_TAG, which includes both the search key and the store/Sent Items folder entry id) using ReportItem.PropertyAccess.GetProperty and try to find a matching message in the Sent Items folder. You can see these properties in OutlookSpy (I am its author).
    Keep in mind that OOM does not allow to search on the binary (PT_BINARY) properties in Items.Find/Restrict.

    If using Redemption is an option (I am also its author), it exposes RDOReportItem.FindOriginalItem method.

    Once you have the original item, you can make a copy and try to send it again.