Last reviewed on February 15, 2015   —  15 Comments

I need to send message from the certain E-mail address (E-mail Distribution Group) with VBA code. I have permission "Send as" and I can do it by hands, changing field "From".

Use SentOnBehalfOfName, and as long as your Exchange account has SendAs permission for the shared mailbox or distribution group, it will be sent from the shared account or group, not sent on behalf of.

Send as using VBA

If you want to send from another account in your Account list, you'll use SendUsingAccount and select the account by index or by name. See Macros to send message using the default account or a specific account for code samples.

SendAs an address code Sample

Note: this macro is used with Exchange account to send from addresses you have SendAs permissions.

Sub CustomMailMessage()
 
Dim OutApp As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Recipient
Dim Recipients As Recipients
 
Set OutApp = CreateObject("Outlook.Application")
Set objOutlookMsg = OutApp.CreateItem(olMailItem)
 
Set Recipients = objOutlookMsg.Recipients
Set objOutlookRecip = Recipients.Add("alias@domain.com")
objOutlookRecip.Type = 1
  
objOutlookMsg.SentOnBehalfOfName = "sales@domain.com"
 
objOutlookMsg.Subject = "Testing this macro"
 
objOutlookMsg.HTMLBody = "Testing this macro" & vbCrLf & vbCrLf

'Resolve each Recipient's name.
For Each objOutlookRecip In objOutlookMsg.Recipients
objOutlookRecip.Resolve
Next
 
'objOutlookMsg.Send
objOutlookMsg.Display
 
Set OutApp = Nothing
 
End Sub

Comments

  1. Chris says

    looks like the

    objOutlookmsg.MailItem.Sender = "sendas_address@domain.com"

    is all a bit wrong, and since this is the crux of this post, I think its a biggie.

    Since the objOutlookmsg is already set as a MailItem object, there is no MailItem property or method - so objOutlookmsg.MailItem wont work.

    If you ignore the MailItem as a typo, then

    objOutlookmsg.Sender = "sendas_address@domain.com"

    still wont work.

    The line results in a type mismatch. The Sender property is of type AddressEntry, but you are simply setting this as a string.

    I actually want to achive this, and although I can see whats wrong, I don't know how to get an AddressEntry object or set the Sender property.

    Help please required

  2. Chris says

    Yes. You simply have a type mismatch here.

    What can work is:

    objOutlookmsg.SentOnBehalfOfName = "foo@bar.com"

    But this only works on exchange I believe. the .Sender is user when you have multiple accounts setup in Outlook. I have seen code the enumerates the accounts and gets the AddressEntry, but maybe you simply want to program a specific AddressEntry - I'm not sure how to set this.

  3. Ebony Rivera says

    Could you please help me. I send a repetitive email at least 20 times per day. I need a VBA code that will simply generate the message with it's contents. Does that make any since? I've been at this with no luck for 2 days. Please help me.......

    • Diane Poremsky says

      You could use a template... or Forward a Draft.

      The basics for creating the message via VBA:
      Sub CreateMail()

      Dim OMail As Outlook.mailItem
      Set OMail = Application.CreateItem(olMailItem)

      With OMail
      .To = "me@here.com"
      .Subject = "The Subject"
      .Body = "The body"
      .Display ' use .send to send it automatically
      End With

      End Sub

  4. Andry says

    I'd be glad to assist. Please copy and paste the text you want o send and I'll embed, or just use the example below:

    ================================================================
    Sub SendEmail()
    Dim objEmail As Outlook.MailItem
    Set objMailApp = CreateObject("Outlook.Application")
    Set objEmail = objMailApp.CreateItem(0)
    With objEmail
    .Subject = "Repetitive Email SUBJECT"
    .To = "me@mydomain.com"
    .CC = "you1@mydomain.com; you2@mydomain.com"
    .BCC = "you3@mydomain.com"
    .HTMLBody = "Hey guys,This email is now automatedThx,Ebony Rivera"
    .BodyFormat = olFormatHTML
    .Attachments.Add ("C:\My Documents\Picture.jpg") 'if you want to attach a file
    .DeleteAfterSubmit = True 'if you want to delete the copy the system leaves in your SENT TO folder use =True, or if not, use =False
    .ExpiryTime = Now() + 1 'expires in 1 day - delete line if you don't want the email to expire; change "1" to "X" if you want "X" days expiration
    .Importance = olImportanceHigh ' if you want to send as HIGH IMPORTANCE
    .Send
    End With
    Set objMailApp = Nothing
    Set objEmail = Nothing
    End Sub
    ================================================================

  5. ali00000a says

    This doesnt work on outlook 2010, you need to use the SentOnBehalfOfName attribute instead of Send.

  6. Hakan says

    HOW CAN I SELECT LAST RECEIVED EMAIL AND SAVE IT TO LOCAL FOLDER? :)
    YOU MAY FIND MY CODE BELOW. THANKS..

    Sub SaveSelectedMailAsTxtFile(item As Outlook.MailItem)

    Const OLTXT = 0
    Dim currentExplorer As Explorer
    Dim Selection As Selection
    Dim oMail As Outlook.MailItem
    Dim obj As Object
    Dim sPath As String
    Dim dtDate As Date
    Dim sName As String

    Set currentExplorer = Application.ActiveExplorer
    Set Selection = currentExplorer.Selection

    For Each obj In Selection
    Set oMail = obj
    ' sName = oMail.Subject
    'ReplaceCharsForFileName sName, "_"

    'dtDate = oMail.ReceivedTime
    'sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".txt"
    sName = "Fuel" & Date & ".txt"
    oMail.SaveAs "C:\Users\hgokceoglu\Desktop\test\" & sName, OLTXT

    Next
    MsgBox "rule çalıştı"

    End Sub

    • Diane Poremsky says

      if you want to use it "manually" by selecting the message then running the macro, you need to change this:
      Sub SaveSelectedMailAsTxtFile(item As Outlook.MailItem)

      to
      Sub SaveSelectedMailAsTxtFile()
      dim item As Outlook.MailItem

  7. mrsklb says

    Hi Diane,
    This looks perfect.
    We have a helpdesk and the 'user' is the person that sends in the email, I want to be able to forward email I receive directly to the helpdesk but define the 'sender' as the customer so the ticket is created in their email account, not mine.

    Is this possible using this code teamed with a 'forward email' code?
    I've seen one elsewhere but it will always send as me, not as the originiator which is not what I need.

    When the email hits the helpdesk the helpdesk auto responds with ticketID etc, which the customer needs for follow up.

    • Diane Poremsky says

      Are you using Exchange Server? if so, then no, you can resend/redirect as the original sender because you need to have send as permission for that address.

      Your options are limited to forward as an attachment (and help desk takes the attachment off the message and works with it) or the help desk uses code to scrape the original sender from the forward. Do you use specialized help desk software or Outlook?

  8. Karen says

    Thanks Diane, sorry, I registered under the forum as MrsAdmin and didn't update here that we've been working on it. I have to update that thread too tomorrow (my 12yr old has been sick & I haven't had a chance to catch up on my topics). Thank you for getting back to me :)

  9. Mark says

    When sending emails from VBA, is there any way to check programmatically if email failed to get through (say, could not connect to Exchange server, or any other reason...)?

Leave a Reply

Please post long or more complicated questions at OutlookForums by Slipstick.com.

If the Post Comment button disappears, press your Tab key.