Home > Back-end >  Problems with changing sender field and account in emails received from specific account
Problems with changing sender field and account in emails received from specific account

Time:02-03

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related