首页 > 解决方案 > 通过 VBA 在撰写模式下在 Outlook 上返回发件人/发件人地址

问题描述

我在 Outlook 上只有一个交换帐户,可以访问两个由管理员创建的共享/组邮箱或邮件地址。我只是该共享邮件组的成员,我可以通过它们发送和接收邮件。所以有时我会因为选择错误的发件人地址而犯错误。在发送电子邮件之前,我需要检查发件人/发件人地址和收件人。但是,我无法通过 VBA 将发件人/发件人地址作为字符串返回。我通过“来自”下拉菜单选择它们。

这是我的代码:

Dim Recipients As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim i
Dim prompt As String
Dim Send_Address As String
Dim checker As Integer

Send_Address = Item.SendUsingAccount.SmtpAddress

checker = 0

Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
    Set recip = Recipients.Item(i)

    If InStr(LCase(recip.Address), "abc.com") Then
    checker = 1
    End If

    If InStr(LCase(recip.Address), "xyz.com") Then
    checker = 2
    End If

Next i

If (Send_Address = "abc.om") And (checker = 1) Then
    MsgBox ("Please check CC list. It includes different clients.")
    Cancel = True
ElseIf (Send_Address = "xyz.com") And (checker = 2) Then
    MsgBox ("Please check CC list. It includes different clients.")
    Cancel = True
Else
    MsgBox ("OK" & checker & Send_Address)
    Cancel = True
End If

End Sub

标签: vbaoutlook

解决方案


From 值在.SentOnBehalfOfName. 它不会在 GUI 更改时更新。

用代码更新值。GUI 将保持不变。

Option Explicit

Sub updateFromValue()

Dim Item As mailItem

Dim mailbox1 As String
Dim mailbox2 As String
Dim mailbox3 As String

Dim resp As String

' first create a draft for testing
Set Item = ActiveInspector.currentItem
Item.Display

Debug.Print ".SentOnBehalfOfName: " & Item.SentOnBehalfOfName

mailbox1 = "abc.com"
mailbox2 = "xyz.com"
mailbox3 = "default.com"

resp = InputBox("1: " & mailbox1 & vbCr & "2: " & mailbox2 & vbCr & "3: " & mailbox3)

If Len(resp) > 0 Then

    Select Case resp

    Case 1
        Item.SentOnBehalfOfName = mailbox1
        Debug.Print "Item.SentOnBehalfOfName: " & Item.SentOnBehalfOfName
        
    Case 2
        Item.SentOnBehalfOfName = mailbox2
        Debug.Print "Item.SentOnBehalfOfName: " & Item.SentOnBehalfOfName
        
    Case 3
        Item.SentOnBehalfOfName = mailbox3
        Debug.Print "Item.SentOnBehalfOfName: " & Item.SentOnBehalfOfName
        
    Case Else
        Debug.Print "?"
    
    End Select
    
End If

End Sub

现在您可以验证.SentOnBehalfOfNameApplication_ItemSend 中的值。


推荐阅读