簡體   English   中英

通過 VBA 在撰寫模式下返回 Outlook 上的發件人/發件人地址

[英]Returning sender/from address on outlook in compose mode via VBA

我在 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

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

現在您可以驗證 Application_ItemSend 中.SentOnBehalfOfName的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM