Outlook VBA ─ Send Mail in Outbox─ Disable Rule, Send Email, Enable Rule

adavid

Board Regular
Joined
May 28, 2014
Messages
145
I have a rule that delays emails that I send. I want a "Send Now" macro that I can assign a button to that will send the email when clicked. The code below disables the rule.
The problem is that messages sent after the rule is disabled go automatically, but messages that were sent before the rule was disabled stay in the mailbox even after the rule is disabled.

Code:
Sub SendNow()
    Dim olRules As Outlook.Rules
    Dim olRule As Outlook.Rule

    Set olRules = Application.Session.DefaultStore.GetRules
    Set olRule = olRules.Item("DelaySend")
    If olRule.Enabled = True Then olRule.Enabled = False
    If olRule.Enabled = False Then olRule.Enabled = True

    olRules.Save

    Set olRules = Nothing
    Set olRule = Nothing

'Sub toggleScheduledSendReceive()
' this only works in Outlook 2010 and earlier

' the Line1 code must run BEFORE the Line2 .execute command
' if Line1 comes after the .execute it will randomly return the control's PREVIOUS value rather than the TOGGLED value.
        ' this because the .execute runs asynchrously.
       '  Vba starts the .execute, then resumes at line2B
       '  even though the toggle may or may not have finished.

On Error GoTo err_routine

Dim x

Dim OutlookSendReceiveWasPreviouslyDisabled As Boolean
Line1: OutlookSendReceiveWasPreviouslyDisabled = Application.ActiveExplorer().CommandBars("Standard").Controls("Send/Re&ceive").Controls("Send/Recei&ve Settings").Controls("&Disable Scheduled Send/Receive").State

Line2:
Application.ActiveExplorer().CommandBars("Standard").Controls("Send/Re&ceive").Controls("Send/Recei&ve Settings").Controls("&Disable Scheduled Send/Receive").Execute
Line2b:

If OutlookSendReceiveWasPreviouslyDisabled Then
      MsgBox "Send/Receive Enabled."
Else
     MsgBox "Send/Receive Disabled"
End If

exit_sub:
    Exit Sub
err_routine:
MsgBox Error$
Resume exit_sub


End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,225,689
Messages
6,186,446
Members
453,354
Latest member
Shaz_7

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top