0

I'm trying to set up an Outlook macro under the Send button, that sets the DeferredDeliveryTime to current time plus 60 mins.

I've hit Alt+F11, created a module and placed this in a sub:

Sub delay_delivery(ByVal item As Object)
    Dim mailItem As MailFormat
    Set mailItem = item
    mailItem.DeferredDeliveryTime = DateAdd("n", 90, Now)
End Sub

And added a link to the Macro in the Quick Access toolbar, but nothing happens.

Community
  • 1
  • 1
Matt Rogers
  • 109
  • 1
  • 9

2 Answers2

4

I think the problem that you are having is that your procedure is never getting the "item".

So your procedure is defined as

 Sub delay_delivery(ByVal item As Object)

but when you run it, the "item" parameter never makes it to the procedure. I haven't tested your code, but I'm surprised it doesn't throw some sort of error when you run it.

There are a few ways to do what you want, but each has its own problems.

Note that the users will need macros enabled in Outlook for these to work and may be prompted each time they open outlook.

The first one is a lot simpler, but will not allow you to use your macro, and will cause a prompt each time the user sends a message. This needs to be put in "ThisOutlookSession"

Private Sub Application_ItemSend(ByVal olItem As Object, Cancel As Boolean)

     Dim response As Integer
     response = MsgBox("Would you like to send this in an hour?", vbYesNo)
     If response = vbYes Then
     olItem.DeferredDeliveryTime = DateAdd("n", 60, Now)
End If

End Sub    

This captures the send event of emails and will put the delay send property on the email if the user chooses to in the message box.

The second way will allow the person to use the macro, however, if the person has multiple emails open, chooses the macro in one of them, but then sends a different one first, it will put the delayed send property on whichever item is sent after choosing the macro. It does reset the delay delivery to false after an email is sent. To do this add the following code to "ThisOutlookSession"

Option Explicit
Private DelaySend As Boolean 'To store whether user wishes to delay

Private Sub Application_Startup()
    DelaySend = False 'Set delaysend initially to false
End Sub

Sub delay_delivery()
   DelaySend = True 'Set delay send when user clicks macro.
End Sub

Private Sub Application_ItemSend(ByVal olItem As Object, Cancel As Boolean)
    If DelaySend = True Then 'Check if user set delay_send
        olItem.DeferredDeliveryTime = DateAdd("n", 60, Now)
    End If

    DelaySend = False 'Reset DelaySend to False 
End Sub

If you want to do something else, you could consider capturing new emails using the procedure seen here: Capture New Emails

You would then be able to come up with some way to store the emails that have been opened and whether they have been set to "DelaySend" status, but a fully functioning app like this is beyond the scope of this answer.

EDIT: See this question to see how to interact with the currently open email Current Email and then modify to set the delay delivery flag.

SSilk
  • 2,433
  • 7
  • 29
  • 44
OpiesDad
  • 3,385
  • 2
  • 16
  • 31
  • Thanks OpiesDad - hugely appreciated. I went with the first option and it's working a charm. The pop-up supports keyboard short-cuts so the additional time taken to send an email is minimal. – Matt Rogers Jan 05 '16 at 10:11
  • Edit: changed code in example from 90 minutes to 60 to match "Would you like to send this in an hour?" prompt in second snippet. – SSilk Jun 01 '23 at 12:36
1

You could not have put your code as is on the QAT. You have to call this code while passing item.

In ThisOutlookSession:

Private Sub Application_ItemSend(ByVal Item As Object, cancel As Boolean)
   Item.DeferredDeliveryTime = DateAdd("n", 60, Now)
End Sub

The item being sent will be the needed parameter, when you press the Send button.

niton
  • 8,771
  • 21
  • 32
  • 52