vbaemailoutlookuser-accountssend-on-behalf-of

Select a Template Email Already Created and Send Using Specific Account


I want to take my existing code in VBA and add the feature to "Send Using a Specific Account". My Current coding calls the template based on the users location (which works great), but the email send of behalf of the same mailbox.

For example when I run the current macro the "From" will be sent as Toast Los Angeles Sent on Behalf of Toast Los Angeles

I would like the email to be BWS Sent on Behalf of Toast Los Angeles

Any help would be greatly appreaciated.

Sub JobCompletion()
Dim Inbox As Object
Dim MyItem As Object
Dim Region As String
Dim RegionB As String
Dim FormTemplate As String

'This code was replaced by Environ("UserName") to be compatible with Win 10
'Select Case fOSUserName()
Select Case Environ("UserName")
  
  'Set up site according to username for LA
  Case "blue", "red", "pink"
  Region = "Toast Los Angeles"
  FormTemplate = "IPM.Note._LA Pres Center Job Complete Notification - IBD"

  'Set up site according to username for HOU
  Case "black", "brown", "gree"
  Region = "Toast Houston"
  FormTemplate = "IPM.Note._HOU Pres Center Job Complete Notification - IBD"
  
  Case Else
  MsgBox "Please Contact Jacob X to add you to the Macro"
  Exit Sub
  
  End Select
  
        'Check Version of Outlook (2007 vs 2010)
            If Outlook.Application.Version = "12.0.0.6680" Then
                On Error GoTo FolderError:
                Set Inbox = Outlook.Application.GetNamespace("MAPI").Folders("Mailbox - " & Region)
                On Error Resume Next
            Else
                On Error GoTo FolderError:
                Set Inbox = Outlook.Application.GetNamespace("MAPI").Folders(Region).Folders("Inbox")
                On Error Resume Next
            End If
    
            'Open Form From Folder (The Inbox =)
            Set MyItem = Inbox.Items.Add(FormTemplate)
            MyItem.SentOnBehalfOfName = Region
            MyItem.Display
  
Set Inbox = Nothing
Set MyItem = Nothing
     
Exit Sub

End Sub

I tried to implement a macro using the default account, but this code is for new emails and not my existing email template.

Public Sub New_Mail()
Dim olNS As Outlook.NameSpace
Dim oMail As Outlook.MailItem

Set olNS = Application.GetNamespace("MAPI")
Set oMail = Application.CreateItem(olMailItem)

'use first account in list
    oMail.SendUsingAccount = olNS.Accounts.Item(1)
    oMail.Display
      
Set oMail = Nothing
Set olNS = Nothing

End Sub

Solution

  • Sounds like you need to use the SendUsingAccount property of the MailItem class. The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called.

    Set MyItem = Inbox.Items.Add(FormTemplate)
    MyItem.SendUsingAccount = Account
    MyItem.Display
    

    Also you may create the item from a template in the folder of that account in Outlook. Just use Store.GetDefaultFolder instead of Namespace.GetDefaultFolder. The Store.GetDefaultFolder method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.