Problems with changing sender field and account in emails received from specific account

221 Views Asked by At

I have below code which changes sender field and also changes account from which the email is being sent. My account is authorized to send also from this email. Code works well on new emails and also works well replied emails, but does not work well on one of the accounts I have. I have 3 accounts (POP, @outlook.com and Exchange). If email has been previously received from an @outlook.com account (this account is also set as exchange account) or set to be sent from an @outlook.com account it does not do the job. For example: If I open new email, set sending account to [email protected] and then run code it does not work. Similar if I reply to an email, which I have received from [email protected] account the code does not work. Code works well if my email (replied or new) is previously set to be sent from my POP account or from my third-exchange account. The code does not work, if my email (replied or new) is previously set to be sent from the @outlook.com account. Why is the code not working on all accounts?

Sub ChangeSender()
    Dim NewMail As MailItem, oInspector As Inspector
    Set oInspector = Application.ActiveInspector
    If oInspector Is Nothing Then
        MsgBox "No active inspector"
    Else
        Set NewMail = oInspector.CurrentItem
        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            NewMail.SentOnBehalfOfName = "[email protected]"
            Dim oAccount As Outlook.Account
            For Each oAccount In Application.Session.Accounts
                If oAccount.DisplayName = "[email protected]" Then
                    NewMail.SendUsingAccount = oAccount
                End If
            Next
            NewMail.Display           
        End If
    End If
End Sub

In case previously set account is the POP one or the third-exchange account the code works well. In case previously set account is the [email protected] account, the code does not work. In VBA I can see the whole code runs through, but there is no change on the line >>NewMail.SendUsingAccount = oAccount as is in case when working properly. This is my first VBA code, so maybe I am missing some basics.

EDIT: Here is a visual image of what I am trying to do with VBA. Same think as I do with several mouse clicks and some typing: Change the email FROM field and change the account from which I am sending email from. Image

EDIT2: Now I have tried to remove line .SentOnBehalfOfName altogether and the script again works only on POP account and does not work on outlook.com Exchange account. In other words: I left only the line .SendUsingAccount and if email is preset to POP account it changes account correctly, if email is preset to outlook.com exchange account it does not change to the Exchange [email protected] account. Any suggestion why some accounts can be changed to another one, some cannot be changed to another one?

EDIT3: Above is image how I am sending email by hand (mouse/keyboard) with from field to [email protected] and it is being sent through my account [email protected]. I am doing this successfully for several months. For the last 2 weeks I have been using partially successfully above VBA code to achieve the same. VBA code works great if before running the code my email from field is set to my POP account or my Exchange account ([email protected]). Above VBA code does not work in case if before running it, my email from field is set to [email protected] account. There are obviously some restrictions which account can change to which with VBA. When changing by hand, there are no restrictions. I think SentOnBehalfOfName line is not a question any more (it is working great when I change account successfully). Maybe I should make a new question altogether without this line?

2

There are 2 best solutions below

6
Dmitry Streblechenko On

You need to set either SentOnBehalfOfName or SendUsingAccount property, but not both unless you set an SendUsingAccount to an Exchange account and SentOnBehalfOfName to a GAL user name from that Exchange account.

1
Eugene Astafiev On

In the following code:

        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            NewMail.SentOnBehalfOfName = "[email protected]"
            Dim oAccount As Outlook.Account
            For Each oAccount In Application.Session.Accounts
                If oAccount.DisplayName = "[email protected]" Then
                    NewMail.SendUsingAccount = oAccount
                End If
            Next
            NewMail.Display           
        End If

The SentOnBehalfOfName is set without making sure you deal with an Exchange account set for the MailItem.SendUsingAccount property. It seems the chosen account in Outlook (or the default one) doesn't have permissions for sending on behalf of another person. Just need to keep the order of setting properties.

So, let's assume the [email protected] account belongs to Exchange where you have sufficient permissions for sending on behalf of another person. The code should look like that then:

        If NewMail.Sent Then
            MsgBox "This is not an editable email"
        Else
            Dim oAccount As Outlook.Account
            For Each oAccount In Application.Session.Accounts
                If oAccount.DisplayName = "[email protected]" Then
                    NewMail.SendUsingAccount = oAccount
                    ' when you deal with an Exchange account 
                    NewMail.SentOnBehalfOfName = "[email protected]"
                End If
            Next
            NewMail.Display           
        End If

You can't set the SentOnBehalfOfName for every account in Outlook, it does make sense only for Exchange with sufficient permissions. So, I'd suggest using the Account.AccountType property for checking the account type and differentiate cases where you can use one or the other properties.