在发送电子邮件之前禁用 'Rule',然后在发送电子邮件后重新启用规则

Disabling a 'Rule' before sending an email, and then re-enabling the Rule after the email was sent

我有一个“Oh Shi-”规则,它会将我的电子邮件延迟 5 分钟送达。 每当我希望电子邮件尽快到达收件人时,我都会禁用此规则。发送电子邮件后重新启用规则。

通过“消息”功能区上的按钮触发宏。

有时会立即发送电子邮件。

有时电子邮件会放在发件箱中。如果我在 选项功能区 → 延迟交付 下进行检查,则会设置 5 分钟的延迟。 5 分钟规则应该已被禁用。

下面是我的代码:

Sub OhShi()
    Dim olRules As Outlook.Rules
    Dim olRule As Outlook.Rule
    Dim intCount As Integer
    Dim blnExecute As Boolean


    Dim objApp As Outlook.Application
    Set objApp = Application

    Dim answer As VbMsgBoxResult
    answer = MsgBox("Are you sure you want to fast-send the email?", vbYesNoCancel + vbQuestion + vbDefaultButton1, "The OhShi- rule is being disabled!")
    If answer = vbYes Then
        Cancel = False
    ElseIf answer = vbNo Then
        Cancel = True
    Else
        Cancel = True
    End If


    If Cancel = False Then
        'disable the rule
         
        Set olRules = Application.Session.DefaultStore.GetRules
        Set olRule = olRules.Item("Delay Delivery 5min")
        olRule.Enabled = False
        If blnExecute Then olRule.Execute ShowProgress:=True
        olRules.Save
         
        'check if rule was indeed disabled
        If olRule.Enabled = False Then
            'send active email
            Set objItem = objApp.ActiveInspector.CurrentItem
            objItem.Send
        Else
            MsgBox "The script failed successfully!", vbCritical
        End If
        
        'reenable the rule
        olRule.Enabled = True
        If blnExecute Then olRule.Execute ShowProgress:=True
        olRules.Save
        
        Set olRules = Nothing
        Set olRule = Nothing
        Set objItem = Nothing
        
    End If

End Sub

我刚刚尝试删除重新启用规则的代码部分,但该规则仍在应用于电子邮件。在代码为 运行 之后,我现在可以验证该规则确实已被禁用,但它仍会应用于电子邮件。

如果忽略禁用规则,可能是发送项目时未同步某些内容。

发送前等待可能会增加可靠性。

Sub disableRule()

    Dim olRules As Rules
    Dim olRuleName As String
    Dim olRule As Rule
    
    Dim objItem As Object
    
    Dim answer As VbMsgBoxResult
    Dim msg As String
    
    Set olRules = Session.defaultStore.GetRules
    olRuleName = "Delay Delivery 5min"
    Set olRule = olRules.Item(olRuleName)
    
    Set objItem = ActiveInspector.currentItem
        
    answer = MsgBox("Are you sure you want to fast-send the email?", _
      vbOKCancel + vbQuestion + vbDefaultButton2, _
      "The " & olRuleName & " rule is being disabled!")
    
    If answer = vbOK Then
    
        ' disable the rule
        olRule.Enabled = False
        olRules.Save
        Debug.Print vbCr & "Rule disabled."
         
        ' If synching is the cause
        Dim waitTime As Long
        Dim delay As Date
        
        ' If it can takes minutes, occasionally failing to send fast may be preferable.
        waitTime = 3    ' in seconds - adjust as needed
        Debug.Print vbCr & "Wait start: " & Now
        
        delay = DateAdd("s", waitTime, Now)
        Debug.Print "Wait until: " & delay
        
        Do Until Now > delay
            DoEvents
        Loop
        
        Debug.Print "Wait end..: " & Now
        Debug.Print "At least " & waitTime & " seconds delay to allow a synch someplace."
        
        ' send active email
        objItem.Send
        Debug.Print vbCr & "objItem.Send"
        
        msg = "Item sent with disabled rule possiby synched."
        Debug.Print msg
        MsgBox msg, vbInformation
        
        olRule.Enabled = True
        olRules.Save
        Debug.Print vbCr & "Rule re-enabled."
        
    Else
        msg = "Item not sent."
        Debug.Print msg
        MsgBox msg, vbInformation
        
    End If
    
    Debug.Print "Done."
    
End Sub