Outlook宏检查在发送

时间:2018-03-21 20:39:30

标签: vba email outlook outlook-vba

我在Outlook中使用多个帐户,因此从许多不同的电子邮件地址发送。如果我从一个我不应该发送的地址发送,我想给出一个警告框。我有两个地址,我永远不会发送(他们只接收帐户。我不想意外地从这些帐户发送。

这个例子几乎就是我要找的。 Example - Checking the "To" address.

我相信使用字符串比较(StrComp)和Item.SenderEmailAddress是我需要的。但是我似乎无法让它发挥作用。

以下是我目前为单个电子邮件地址发出警告的尝试(bad.email@gmail.com)。

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

On Error Resume Next
 ' use lower case for the address
 ' LCase converts all addresses in the To field to lower case
If StrComp((Item.SenderEmailAddress), "bad.email@gmail.com") Then
    Exit Sub
End If

        Prompt$ = "You sending this from " & Item.SenderEmailAddress & ". Are you sure you want to send it?"
       If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
         Cancel = True
       End If

End Sub

理想情况下,我可以使用相同的代码检查两个或更多地址。使用他们在该示例中所做的事情应该有效。

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    On Error Resume Next
   Select Case LCase(Item.To)
    Case "alias@domain.com", "alias2@domain3.com", "alias3@domain3.com"
        Item.Send
    Case Else
      Prompt$ = "You are not sending this to " & Item.To & ". Are you sure you want to send the Mail?"
       If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
         Cancel = True
       End If

    End Select

End Sub

此外,我在哪里放置代码以确保它始终正常运行并准备就绪?

2 个答案:

答案 0 :(得分:0)

尝试使用SendUsingAccount - 如上所述,SenderEmailAddress在未发送的邮件中不存在。

Option Explicit

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

Dim Send_Address As String
Dim Prompt As String

' Check Send_from name
Send_Address = Item.SendUsingAccount.SmtpAddress

Select Case Send_Address
    Case "alias@domain.com", "alias2@domain3.com", "alias3@domain3.com"
        Item.Send
    Case Else

        Prompt = "You are currently sending this email from " & Send_Address & ". Are you sure you want to proceed?"
        If MsgBox(Prompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
            Cancel = True
        End If

End Select

End Sub

您应该粘贴此代码" ThisOutlookSession,"在VBA编辑器中的Microsoft Outlook对象下。

答案 1 :(得分:0)

我认为应该是MailItem.SendUsingAccount Property (Outlook)

  

返回或设置一个Account对象,该对象表示要在其下发送MailItem的帐户。读/写,另见MailItem.SendUsingAccount Property.

示例

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim Prompt As String
        Prompt = "Are you sure you want to send from 0m3r@Email.com?"

        If Item.SendUsingAccount = "0m3r@Email.com" Then
            If MsgBox(Prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
                Cancel = True
            End If
        End If

End Sub