0

I found a script online to resend emails stuck in the Outbox in Outlook 365. It requires that I select the emails.

I have two accounts, including a Microsoft Exchange account. Is there any way to modify this macro so that when I send an email in the Microsoft Exchange account, all emails which may be stuck in the Outbox, are automatically selected and the below macro is run?

Sub BatchResendEmails()
    Dim objSelection As Outlook.Selection
    Dim objMail As Outlook.MailItem
    Dim objInspector As Outlook.Inspector
    Dim objResendMail As Outlook.MailItem

    Set objSelection = Application.ActiveExplorer.Selection
 
    If Not (objSelection Is Nothing) Then
        On Error Resume Next
        For Each objMail In objSelection
            objMail.Display
            Set objInspector = myItem.GetInspector
 
            'Resend message
            objInspector.CommandBars.ExecuteMso ("ResendThisMessage")
  
            Set objResendMail = Application.ActiveInspector.CurrentItem
 
            'You can change the email details as per your needs
            With objResendMail
                .Subject = objMail.Subject
                .Send
            End With
 
            objMail.Close olDiscard
        Next
    End If
End Sub
Community
  • 1
  • 1

2 Answers2

0

Off the top of my head:

    Sub BatchResendEmails()
    Dim objFolder As Outlook.MAPIFolder
    Dim objMail As Outlook.MailItem
    Dim objInspector As Outlook.Inspector
    Dim objResendMail As Outlook.MailItem

    Set objFolder = Application.Session.GetDefaultFolder(olFolderOutbox)
 
       On Error Resume Next
       For Each objMail In objFolder.Items
           objMail.Display
           Set objInspector = myItem.GetInspector
 
           'Resend message
           objInspector.CommandBars.ExecuteMso ("ResendThisMessage")
  
           Set objResendMail = Application.ActiveInspector.CurrentItem
 
           'You can change the email details as per your needs
           With objResendMail
               .Subject = objMail.Subject
               .Send
           End With
 
           objMail.Close olDiscard
       Next
End Sub
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Thanks, but it comes back with a Run Time error '438" Object doesn't support this property or method – Legaldeejay Feb 20 '21 at 02:26
  • See the change above – Dmitry Streblechenko Feb 20 '21 at 03:33
  • Thanks. It works when I run the script manually. But is there a way for this script to automatically run whenever I send an email in the Exchange account? – Legaldeejay Feb 20 '21 at 05:00
  • To be more specific, I want this script to run automatically whenever I click the Send button in the Exchange account. Thanks. – Legaldeejay Feb 20 '21 at 05:14
  • Alternatively, I would like to schedule the script to run periodically (i.e. every 5 minutes) using Windows Task Scheduler or running a batch file. I appreciate your assistance. – Legaldeejay Feb 20 '21 at 14:05
  • There is nothing out of the box that will let you run it automatically. – Dmitry Streblechenko Feb 20 '21 at 18:17
  • OK, then how can I schedule this script to run every 5 minutes? I have read about using task scheduler and creating a vbs or bat file, but not sure how to do that since this is VBA code. – Legaldeejay Feb 20 '21 at 20:35
  • That's what I meant- automatically means "on a timer". Task scheduler does not work too well with Outlook scripts. You can create a COM addin, then you can run that code (or similar) whenever your want. – Dmitry Streblechenko Feb 20 '21 at 22:38
0

To remove the manual selection of the folder set a reference to the folder https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.getdefaultfolder.

To remove the manual selection of items Iterate all email items in a specific Outlook folder.
You may need a reverse loop when decreasing the number of items in the outbox For Each loop: Some items get skipped when looping through Outlook mailbox to delete items.

To run code periodically Outlook VBA - Run a code every half an hour.

niton
  • 8,771
  • 21
  • 32
  • 52