+ Reply to Thread
Results 1 to 10 of 10

Macro to Send Email from different account

Hybrid View

automafia Macro to Send Email from... 03-04-2015, 03:16 PM
jaslake Re: Macro to Send Email from... 03-04-2015, 03:20 PM
automafia Re: Macro to Send Email from... 03-04-2015, 03:22 PM
jaslake Re: Macro to Send Email from... 03-04-2015, 04:15 PM
automafia Re: Macro to Send Email from... 03-04-2015, 04:17 PM
automafia Re: Macro to Send Email from... 03-04-2015, 04:22 PM
jaslake Re: Macro to Send Email from... 03-04-2015, 04:28 PM
automafia Re: Macro to Send Email from... 03-04-2015, 04:32 PM
automafia Re: Macro to Send Email from... 03-04-2015, 04:54 PM
jaslake Re: Macro to Send Email from... 03-04-2015, 04:59 PM
  1. #1
    Registered User
    Join Date
    03-04-2015
    Location
    MT
    MS-Off Ver
    2013
    Posts
    9

    Exclamation Macro to Send Email from different account

    Hello,

    I have Office 2013, and I am trying to write a macro to send an email from a certain account, but my Email_Send_From doesnt seem to be working. The email sends to who I want it to, and says what I want it to, but it is using the wrong email to send from.


    Any help? I have several emails setup on my outlook. I want it to send from my gmail account. I have it setup with imap.

    Thank you for the help.

    Sub Email_Tenant_1934()
    Dim Email_Subject, Email_Send_From, Email_Send_To, _
    Email_Cc, Email_Bcc, Email_Body As String
    Dim Mail_Object, Mail_Single As Variant
    Email_Subject = "Hello " & ActiveSheet.Range("B3")
    Email_Send_From = "XXXX@gmail.com"
    Email_Send_To = ActiveSheet.Range("B5")
    Email_Cc = ""
    Email_Bcc = ""
    Email_Body = "It works!"
    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)
    With Mail_Single
    .Subject = Email_Subject
    .To = Email_Send_To
    .cc = Email_Cc
    .BCC = Email_Bcc
    .Body = Email_Body
    .send
    
    MsgBox "Mail Has Been Sent"
    
    End With
    debugs:
    If Err.Description <> "" Then MsgBox Err.Description
    End Sub
    Last edited by automafia; 03-04-2015 at 04:57 PM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Send Email from different account

    Hi automafia

    Welcome to the Forum!!!

    Please wrap your Code in Code Tags...it's a Forum requirement.

    Do a search on "excel vba send email on behalf of"
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    03-04-2015
    Location
    MT
    MS-Off Ver
    2013
    Posts
    9

    Re: Macro to Send Email from different account

    Sorry about that. I have been searching the internet all morning on this and I havent found anything that has helped so far.

    It is still sending from my main work account instead of the email I want it to send from.
    Last edited by automafia; 03-04-2015 at 03:25 PM.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Send Email from different account

    Hi automafia

    This Code works for me...
    Option Explicit
    
    Sub Email_Tenant_1934()
       Dim Email_Subject, Email_Send_From, Email_Send_To, _
           Email_Cc, Email_Bcc, Email_Body As String
       Dim Mail_Object, Mail_Single As Variant
       Email_Subject = "Hello " & ActiveSheet.Range("B3")
       Email_Send_From = "XXXX@gmail.com"
       Email_Send_To = ActiveSheet.Range("B5")
       Email_Cc = ""
       Email_Bcc = ""
       Email_Body = "It works!"
       On Error GoTo debugs
       Set Mail_Object = CreateObject("Outlook.Application")
       Set Mail_Single = Mail_Object.CreateItem(0)
       With Mail_Single
          .SentOnBehalfOfName = "foo@bar.com"  '<---This works for me
          .Subject = Email_Subject
          .To = Email_Send_To
          .cc = Email_Cc
          .BCC = Email_Bcc
          .Body = Email_Body
          .Display
    
          MsgBox "Mail Has Been Sent"
    
       End With
    debugs:
       If Err.Description <> "" Then MsgBox Err.Description
    End Sub

  5. #5
    Registered User
    Join Date
    03-04-2015
    Location
    MT
    MS-Off Ver
    2013
    Posts
    9

    Re: Macro to Send Email from different account

    Okay let me try that real quick
    Last edited by automafia; 03-04-2015 at 04:19 PM.

  6. #6
    Registered User
    Join Date
    03-04-2015
    Location
    MT
    MS-Off Ver
    2013
    Posts
    9

    Re: Macro to Send Email from different account

    I get the following email when I try to send that.


    Your message did not reach some or all of the intended recipients.

    Subject: Hello Megan Schuelka
    Sent: 3/4/2015 1:21 PM

    The following recipient(s) cannot be reached:

    Wes Smith on 3/4/2015 1:21 PM
    This message could not be sent. Try sending the message again later, or contact your network administrator. Error is [0x80070005-00000000-00000000].

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Send Email from different account

    No clue...Google it.

  8. #8
    Registered User
    Join Date
    03-04-2015
    Location
    MT
    MS-Off Ver
    2013
    Posts
    9

    Re: Macro to Send Email from different account

    Weird. I get that error if I just hit send. But if I manually click the From: and then select the account I want to use it works. It looks the exact same before and after I select it.

  9. #9
    Registered User
    Join Date
    03-04-2015
    Location
    MT
    MS-Off Ver
    2013
    Posts
    9

    Re: Macro to Send Email from different account

    I have figured it out!


    Here is the solution.

    After adding my Gmail account to Outlook, I closed outlook at restarted it.

    I then ran this Macro:

    Sub Which_Account_Number()
    'Don't forget to set a reference to Outlook in the VBA editor
        Dim OutApp As Outlook.Application
        Dim I As Long
    
        Set OutApp = CreateObject("Outlook.Application")
    
        For I = 1 To OutApp.Session.Accounts.Count
            MsgBox OutApp.Session.Accounts.Item(I) & " : This is account number " & I
        Next I
    End Sub

    Using this Macro I found that Outlook saw my gmail as Account Number 2.

    I used the following code to send the email:

    Sub Mail_small_Text_Change_Account()
    'Only working in Office 2007-2013
    'Don't forget to set a reference to Outlook in the VBA editor
        Dim OutApp As Outlook.Application
        Dim OutMail As Outlook.MailItem
        Dim strbody As String
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(olMailItem)
    
        strbody = "Hi there" & vbNewLine & vbNewLine & _
                  "This is line 1" & vbNewLine & _
                  "This is line 2" & vbNewLine & _
                  "This is line 3" & vbNewLine & _
                  "This is line 4"
    
        On Error Resume Next
        With OutMail
            .To = "xxxxxx.xxx@xxxx.com"
            .CC = ""
            .BCC = ""
            .Subject = "This is the Subject line"
            .Body = strbody
    
            'SendUsingAccount is new in Office 2007
            'Change Item(1)to the account number that you want to use
           .SendUsingAccount = OutApp.Session.Accounts.Item(2)
    
            .Display
            
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to Send Email from different account

    Great work...I misunderstood your requirements.

    You taught me something new today...Thank You.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Send email from non-default outlook account via VBA
    By moonmanhk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-26-2014, 10:44 AM
  2. Excel Email Macro VBA - How to send an email once all required fields have been fille
    By wallacm2009 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2012, 10:51 AM
  3. Replies: 6
    Last Post: 12-02-2011, 02:14 PM
  4. Send data from Excel in email from specific email account
    By eriknokc in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-05-2007, 05:02 PM
  5. Email Macro using IMAP account
    By billy2willy in forum Excel General
    Replies: 0
    Last Post: 08-17-2005, 12:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1