0
votes

I have just one exchange account on outlook and access to two shared/group mailbox or mail address which are created by admin. I am just a member of that shared mail group and I can send and receive mails via them. So sometimes I make a mistake by choosing wrong sender address. I need to check sender/from address and recipients before sending emails. However, I can't return sender/from addresses as a string via VBA. I choose them by 'from' drop down menu.

Here is my code:

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
1
What is wrong with using SendUsingAccount property? - Dmitry Streblechenko

1 Answers

0
votes

The From value is in .SentOnBehalfOfName. It is not updated on a GUI change.

Update the value with code. The GUI will remain unchanged.

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

Now you can verify the value of .SentOnBehalfOfName in Application_ItemSend.