Search code examples
vbaoutlooksend-on-behalf-of

Always CC when sending "On Behalf Of"


I often send emails on behalf of another user. I'd like to use VBA to automatically CC that user every time I send an email from/on behalf of that user.

I'm not familiar with VBA for Outlook but I'm thinking you could write an if statement that says "if sending message from UserX, cc UserX". The code should run automatically any time an email is sent on behalf.


Solution

  • SentOnBehalfOfName is tricky. It is usually empty until the item has been sent.

    With this code in ThisOutlookSession you should find it blank.

    Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
    
        Dim myRecipient As Recipient
    
        Debug.Print " item.SentOnBehalfOfName - " & item.SentOnBehalfOfName
    
        If item.SentOnBehalfOfName = "someone@somewhere.com" Then
            Set myRecipient = item.Recipients.Add("Someone Else")
            myRecipient.Type = olCC
            item.Recipients.ResolveAll
        End If
    
    End Sub
    

    At least one way to get around this:

    Sub createSentOnBehalf()
    
        Dim objMsg As mailitem
    
        Set objMsg = Application.CreateItem(olMailItem)
        objMsg.SentOnBehalfOfName = "someone@somewhere.com"
        objMsg.Display
    
        Set objMsg = Nothing
    
    End Sub
    
    Sub replySentOnBehalf()
    
        Dim objMsg As mailitem
    
        Set objMsg = ActiveInspector.currentItem.reply
        objMsg.SentOnBehalfOfName = "someone@somewhere.com"
        objMsg.Display
    
        Set objMsg = Nothing
    
    End Sub
    

    Edit: Just realized you could set the cc while creating / replying rather than waiting until ItemSend.

    Edit2: Move the cc code from itemsend

    Sub createSentOnBehalf()
    
        Dim objMsg As mailitem
        Dim myRecipient As Recipient
    
        Set objMsg = Application.CreateItem(olMailItem)
        objMsg.SentOnBehalfOfName = "someone@somewhere.com"
    
        Set myRecipient = objMsg.Recipients.Add("Someone Else")
        myRecipient.Type = olCC
        objMsg.Recipients.ResolveAll
    
        objMsg.Display
    
        Set objMsg = Nothing
    
    End Sub