VBA to remove deferred delivery on a MeetingItem

Status
Not open for further replies.

Michael Nagy

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2010
To prevent compliance breeches I'm writing some code to delay sending of emails to external recipients.
A familiar problem but solutions I've seen only address email but no other Outlook types.

Most of my code works OK but I am having trouble removing the delay on a delayed MeetingItem.

Urgent items can be sent immediately by re-sending them again from the Outbox. The code should remove any existing delay, therefore sending immediately.

The following works for MailItems but fails for MeetingItem as meeting updates don't seem to inherit the existing delay.

Dim objNamespace As Outlook.NameSpace
Dim objItem As Object ' can be MailItem, MeetingItem etc
Dim objRecips As Outlook.Recipients
Dim objAttachments As Outlook.Attachments

Dim iCounter As Integer
Dim strMsgExtRecips As String
Dim strMsgAttachments As String
Dim strPrompt As String

Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Const NoDeferredDelivery As String = "1/01/4501" ' Magic number Outlook uses for "delay mail box isn't checked"

Set objNamespace = Application.GetNamespace("MAPI")
Set objItem = Item 'Set object to the email just sent

' If item is currently delayed, remove the delay and quit.
If objItem.DeferredDeliveryTime <> NoDeferredDelivery Then
strPrompt = "Do you want to send immediately?"
If (MsgBox(strPrompt, vbYesNo + vbInformation + vbMsgBoxSetForeground, "Delivery delay will be removed") = vbYes) Then
objItem.DeferredDeliveryTime = NoDeferredDelivery
GoTo Procedure_Exit
Else
Cancel = True
GoTo Procedure_Exit
End If
End If

Any pointers greatly appreciated.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
How are you adding the delay? It works using a rule or using a send macro -
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Defer delivery time
   SendAt = DateSerial(Year(Now), Month(Now), Day(Now) + 1) + #7:00:00 AM#
Item.DeferredDeliveryTime = SendAt
End Sub
 

Michael Nagy

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2010
Thanks for the replying Diane. First post even though Slipstick has been my go to website for Outlook VBA for years.

That's exactly the way I'm adding the delay. But I'm having trouble removing the delay for Meeting Items.
Currently I imagine users will go to the outbox and be able to immediately send any delayed items they find there if it's urgent.
I suspect I don't understand the behaviour of meeting items well enough.

I call this from Application_ItemSend:

Public Sub OutgoingExternalEmails(ByVal Item As Object, Cancel As Boolean)

' Assists in preventing compliance breaches when emails are sent to incorrect recipients
' - Presents a warning message before sending an email to an external address
' - Delays sending of external emails by a set time

Dim objNamespace As Outlook.NameSpace
Dim objItem As Object ' can be MailItem, MeetingItem etc
Dim objRecips As Outlook.Recipients
Dim objAttachments As Outlook.Attachments

Dim iCounter As Integer
Dim strMsgExtRecips As String
Dim strMsgAttachments As String
Dim strPrompt As String

Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Const NoDeferredDelivery As String = "1/01/4501" ' Magic number Outlook uses for "delay mail box isn't checked"
Const strSecurityGroup As String = "MEL_Outlook2010_PopUpMsg_Enable" ' members of this group are internal recipients. (not used at the moment)
Const iDeferredDeliveryTime As Integer = 5 ' delivery delay in minutes
Const boolDebugMode As Boolean = False ' Set to False when in production. Set to True only when testing and debugging the macro.

On Error GoTo Procedure_Error

Set objNamespace = Application.GetNamespace("MAPI")

Set objItem = Item 'Set object to the email just sent

Select Case TypeName(objItem)

Case "MailItem", "MeetingItem"

' If item is currently delayed, remove the delay and quit.
' This allows a user to go to their Outbox and send items immediately. This method is to be used for urgent items.
If objItem.DeferredDeliveryTime <> NoDeferredDelivery Then
strPrompt = "Do you want to send immediately?"
If (MsgBox(strPrompt, vbYesNo + vbInformation + vbMsgBoxSetForeground, "Delivery delay will be removed") = vbYes) Then
objItem.DeferredDeliveryTime = NoDeferredDelivery
GoTo Procedure_Exit
Else
Cancel = True
GoTo Procedure_Exit
End If
End If

' Get external recipients.
Set objRecips = objItem.Recipients
For iCounter = 1 To objRecips.Count
If objRecips.Item(iCounter).AddressEntry.Type = "SMTP" Then
strMsgExtRecips = strMsgExtRecips & vbNewLine & objRecips.Item(iCounter).Name
End If
Next iCounter

Case Else
MsgBox "Unknown Outlook Item Type"
GoTo Procedure_Exit
End Select

' If no external recipients then exit
If strMsgExtRecips = vbNullString Then GoTo Procedure_Exit

' Get list of attachments
Set objAttachments = objItem.Attachments
If objItem.Attachments.Count > 0 Then
For iCounter = 1 To objAttachments.Count
strMsgAttachments = strMsgAttachments & vbNewLine & objAttachments(iCounter).FileName
Next iCounter
End If

' Present warning message
strPrompt = vbNewLine & "Are the following recipient(s) correct?" & vbNewLine & strMsgExtRecips
strPrompt = strPrompt & vbNewLine

If Not strMsgAttachments = vbNullString Then
strPrompt = strPrompt & vbNewLine & "Are the following attachment(s) correct?" & vbNewLine & strMsgAttachments
Else
End If

If Not (MsgBox(strPrompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "External Email") = vbYes) Then
'user has cancelled message: cancel send and exit
Cancel = True
GoTo Procedure_Exit
End If

' Send with delay
If Not objItem Is Nothing Then
objItem.DeferredDeliveryTime = DateAdd("n", iDeferredDeliveryTime, Now())
End If

Procedure_Exit:
On Error Resume Next
' Clean up.
Set objNamespace = Nothing
Set objItem = Nothing
Set objRecips = Nothing
Set objAttachments = Nothing
Exit Sub

Procedure_Error:
Err.Clear
Cancel = boolDebugMode ' don't send if an error occurs while we are testing the macro but send if in production

Resume Procedure_Exit
End Sub
 
Status
Not open for further replies.
Top