Solved

Excel VBA Macro to send email from a specific Outlook account

Posted on 2014-12-28
8
2,418 Views
Last Modified: 2014-12-29
I have a macro that sends emails.  I need to change the email account that the email is sent FROM.  I have tried the SendOnBehalfOfName and SendUsingAccount parameters but can't get it to work.  I can only get it to send from my default Outlook email account, please advise! NB: I am using Excel and Outlook 2007

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strfrom As String, strto As String, strcc As String, strbcc As String
    Dim strsub As String, strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strfrom = "my@email.com"
    strto = "you@email.com"
    strcc = ""
    strbcc = ""
    strsub = "Body Text"
           
    With OutMail
        .To = strto
        .CC = strcc
        .BCC = strbcc
        .Subject = strsub
        .HTMLBody = strbody
        .Send
    End With
0
Comment
Question by:Kevin Judge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40521408
Hi,

pls try

   
Dim OutApp As Object
    Dim OutMail As Object
    Dim oAccount As Object
    Dim strfrom As String, strto As String, strcc As String, strbcc As String
    Dim strsub As String, strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strfrom = "my@email.com"
    strto = "you@email.com"
    strcc = ""
    strbcc = ""
    strsub = "Body Text"
    For Each oAccount In OutApp.Session.Accounts
        If oAccount.DisplayName = strfrom Then 'OR oAccount.SmtpAddress = strfrom
            With OutMail
                .To = strto
                .CC = strcc
                .BCC = strbcc
                .Subject = strsub
                .HTMLBody = strbody
                .SendUsingAccount = oAccount
                .Send
            End With
        End If
    Next

Open in new window

Regards
0
 

Author Comment

by:Kevin Judge
ID: 40521694
Hi
The loop through outlook accounts works, however, it crashes on the ".SendUsingAccount = oAccount" line regardless of oAccount being set to default or the account I want!!  FYI the outlook email accounts are IMAP.
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40521715
maybe try

    Dim OutApp As Object
    Dim OutMail As Object
    Dim oAccount As Object
    Dim strfrom As String, strto As String, strcc As String, strbcc As String
    Dim strsub As String, strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strfrom = "my@email.com"
    strto = "you@email.com"
    strcc = ""
    strbcc = ""
    strsub = "Body Text"
    For Each oAccount In OutApp.Session.Accounts
        If oAccount.DisplayName = strfrom Then 'OR oAccount.SmtpAddress = strfrom
            With OutMail
                .To = strto
                .CC = strcc
                .BCC = strbcc
                .Subject = strsub
                .HTMLBody = strbody
                 Set .SendUsingAccount = oAccount
                .Send
            End With
        End If
    Next

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kevin Judge
ID: 40521742
"Set .SendUsingAccount = oAccount" did the trick!  However, I have many different emails to send, so do I have to do this loop for each email? Can't I just hard code Set .SendUsingAccount = "emal@domain.com" and avoid repeating the code searching for the email account!?
0
 
LVL 51

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 500 total points
ID: 40521758
you could try ( not sure it works)

      Set .SendUsingAccount = OutApp.Session.Accounts.Item("emal@domain.com")

Regards
0
 

Author Comment

by:Kevin Judge
ID: 40521910
Yes that works!!  So FYI I have stripped out all the other code as it is surplus to requirement and have inserted Set .SendUsingAccount = OutApp.Session.Accounts.Item("emal@domain.com") command before each .send line!

Many thanks for your help and prompt replies, excellebt service.  

PS I am new to Experts Exchange; what do I do next?
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40521923
0
 

Author Closing Comment

by:Kevin Judge
ID: 40521960
Fantastic, thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question