none
Excel VBA sub creating new email from secondary account RRS feed

  • Question

  • I have a new email which i set as the default account, but the following Excel VBA sub is still creating a new email using the old address:

    Public Sub TestEmail()

        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
        Dim theMail As String
        

        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        theMail = grEEt & "," & vbNewLine & vbNewLine & "Please find attached commercial invoice." & vbNewLine
        
        On Error Resume Next
        With OutMail
            .To = "SomeEmailAddress@Domain.com"
            .CC = ""
            .BCC = ""
            .Subject = "TEST"
            .Body = theMail
            .Display
        End With
        
    End Sub

    Thank you for your help!

    Tuesday, February 17, 2015 3:54 PM

Answers

  • Hello Kelaref,

    You need to use the SendUsingAccount property of the MailItem class which allows to set an Account object that represents the account under which the MailItem is to be sent.

    Sub SendUsingAccount()  
     Dim oAccount As Outlook.account  
     For Each oAccount In Application.Session.Accounts  
     If oAccount.AccountType = olPop3 Then  
     Dim oMail As Outlook.MailItem  
     Set oMail = Application.CreateItem(olMailItem)  
     oMail.Subject = "Sent using POP3 Account"  
     oMail.Recipients.Add ("someone@example.com")  
     oMail.Recipients.ResolveAll  
     oMail.SendUsingAccount = oAccount  
     oMail.Send  
     End If  
     Next  
    End Sub 
     

    Tuesday, February 17, 2015 4:00 PM

All replies

  • Hello Kelaref,

    You need to use the SendUsingAccount property of the MailItem class which allows to set an Account object that represents the account under which the MailItem is to be sent.

    Sub SendUsingAccount()  
     Dim oAccount As Outlook.account  
     For Each oAccount In Application.Session.Accounts  
     If oAccount.AccountType = olPop3 Then  
     Dim oMail As Outlook.MailItem  
     Set oMail = Application.CreateItem(olMailItem)  
     oMail.Subject = "Sent using POP3 Account"  
     oMail.Recipients.Add ("someone@example.com")  
     oMail.Recipients.ResolveAll  
     oMail.SendUsingAccount = oAccount  
     oMail.Send  
     End If  
     Next  
    End Sub 
     

    Tuesday, February 17, 2015 4:00 PM
  • Are you working with a different Exchange or a POP3/SMTP account?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, February 17, 2015 5:09 PM