The scenario:
Two teams: MainTeam and HelpingTeam
MainTeam uses a shared mailbox exclusively and a macro sends all emails "on behalf of MainTeam" instead of sending-as the shared mailbox.
HelpingTeam users are going to help the other team. They need to indicate email is sent "on behalf of MainTeam".
The shared mailbox has been added to the users on the HelpingTeam and in a new mail window, the email address for the shared mailbox is below their personal one. Using this "From" address would indicate they are trying to SendAs the mailbox, which we don't want.
I could show them how to add another "From" address and set it up to use their primary account to "SendonBehalfOf", but they do not want to be confused because now they will see two entries in their "From" list: the "SendAs" entry (Fixed, can not be removed) and the "SendonBehalfOf" entry (can be removed).
I am trying to change the email properties so email will be sent on behalf of the shared mailbox.
- When sending the email from the shared mailbox using this macro, everything works.
- When initiating the email from a personal mailbox and changing the sender to the "SendAs" account (the only shared account in the list), the properties in the macro appear to be correct, but Outlook does not process the change and the system denies the message.
I've made so many revisions that I've lost track of what works and what doesn't. Below is the most functional version as described above. The MsgBox entries are to help me keep track of what is going on behind the scenes:
Dim oAccount As Outlook.Account
Dim objItem As MailItem
Dim WithEvents objInspectors As Outlook.Inspectors
Dim WithEvents objMailItem As Outlook.MailItem
Dim WithEvents myOlExp As Outlook.Explorer
Dim Sender As Outlook.AddressEntry
Private Sub Application_Startup()
Initialize_handler
End Sub
Public Sub Initialize_handler()
Set objInspectors = Application.Inspectors
Set myOlExp = Application.ActiveExplorer
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then
Set objMailItem = Inspector.CurrentItem
If objMailItem.Sent = False Then
Call SetFromAddress(objMailItem)
End If
End If
End Sub
Public Sub SetFromAddress(objMailItem As Outlook.MailItem)
'To see which account user is trying to send from
MsgBox "[SetFromAddress] SendUsingAccount: " & objMailItem.SendUsingAccount
MsgBox "[SetFromAddress] SentOnBehalfOfName: " & objMailItem.SentOnBehalfOfName
'Check which account is in focus as primary
If objMailItem.SendUsingAccount = "[email protected]" Then
MsgBox "sendfromaddress if triggered"
'set sender to be the Shared Mailbox
objMailItem.SentOnBehalfOfName = "[email protected]"
'Find Primary O365 account and use that to send the email "on behalf of"
For Each oAccount In Application.Session.Accounts
If oAccount = "[email protected]" Then
objMailItem.SendUsingAccount = oAccount
End If
Next
End If
MsgBox "SetFromAddress Sending As: " & objMailItem.SendUsingAccount
MsgBox "SetFromAddress OnBehalf: " & objMailItem.SentOnBehalfOfName
End Sub
'Below enables Outlook 2013/2016/365 Reading Pane Reply
Private Sub myOlExp_InlineResponse(ByVal objItem As Object)
Call SetFromAddress(objItem)
End Sub
'Added the sub below in case the user manually switchs from personal to shared mailbox
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
MsgBox "[ItemSend] SendUsingAccount: " & Item.SendUsingAccount
MsgBox "[ItemSend] SentOnBehalfOfName: " & Item.SentOnBehalfOfName
'Check if Shared Account
If Item.SentOnBehalfOfName = "[email protected]" Then
MsgBox "If triggered"
'set sender to be the Shared Mailbox
Item.SentOnBehalfOfName = "[email protected]"
'Find Primary O365 account and use that to send the email "on behalf of"
For Each oAccount In Application.Session.Accounts
If oAccount = "[email protected]" Then
Item.SendUsingAccount = oAccount
End If
Next
End If
MsgBox "[ItemSend] SendUsingAccount: " & Item.SendUsingAccount
MsgBox "[ItemSend] SentOnBehalfOfName: " & Item.SentOnBehalfOfName
End Sub
30/11/2020
This is how I am working around the issue for now, but it fails if it's an in-line reply:
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
Dim oAccount As Outlook.Account
Dim objItem As MailItem
'To test later which account user is trying to send from
Set SendingAccount = item.SendUsingAccount
'Check if Shared Account
If SendingAccount = "[email protected]" Then
'Intecept email, stop it from sending, and create a new one "on behalf of"
If TypeOf item Is MailItem Then
Set objItem = item.Copy
item.Delete
Cancel = True
'set sender to be the Shared Mailbox
objItem.SentOnBehalfOfName = "[email protected]"
'Find Primary O365 account and use that to send the email "on behalf of"
For Each oAccount In Application.Session.Accounts
If oAccount = "[email protected]" Then
objItem.SendUsingAccount = oAccount
End If
Next
End If
'send email
objItem.Send
End If
End Sub
.SentOnBehalfOfName
is empty until mail is sent or it is assigned in the code. If important enough, the user would run code to assign it on every mail. Verify if empty in ItemSend then the user would assign a value. – niton