Solved

Excel sending an email

Posted on 2016-10-10
5
72 Views
Last Modified: 2016-10-15
I need to be able to send an email via excel using the contents of a worksheet to populate the email. Then I need to be able to select the outlook mailbox which I wish to send the email from which won't be the default for outlook.
0
Comment
Question by:Sam Coombes
  • 2
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
yo_bee earned 500 total points
ID: 41837714
Here is a module that I use in my weekly reports that need to be e-mailed to all upper management.

Sub Email_Report(ByVal Fname As String)
    'sourced from:Automation of out look Mails based on the excel list
    'http://www.thecodecage.com/forumz/1054996151-post10.html
    'http://www.thecodecage.com/forumz/microsoft-outlook-forum/213688-search-new-email-text-make-bold-blue.html#post1055005238
    'RB: other constants to allow Late Binding*
    'Const olFolderInBox As Long = 6
    Dim OutApp As Object
    Dim oNameSpace As Object
    Dim OutMail As Object

'...other code & then...

    'check for open Outlook session & open if necessary
    'sourced from: Microsoft Office Help - Microsoft Office Discussion - Excel VBA Programming - Access Programming
    On Error Resume Next
    Set OutApp = GetObject(, "Outlook.Application")
    On Error GoTo 0

    '### may need modification
    If OutApp Is Nothing Then
        Set OutApp = CreateObject("Outlook.Application")
        Set oNameSpace = OutApp.GetNamespace("MAPI")
        oNameSpace.Logon , , True
        oNameSpace.GetDefaultFolder(olFolderInBox).Display
        'WasOutlookOpenedByCode = True
    End If

    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)

'Dim OutApp As Object
'Dim OutMail As Object
'Set OutApp = CreateObject("Outlook.Application")
'Set OutMail = OutApp.CreateItem(0)

'    On Error Resume Next
'   ' Change the mail address and subject in the macro before you run it.
   With OutMail
        .SentOnBehalfOfName = "PGA Tour"
        .To = "Mickelson, Phil; Woods,Tiger"
        .CC = "Fowler, Rickie"
        .BCC = "Nicklaus, Jack" '
        .Subject = "Golf Weekly Update"
        .Body = "Blah, Blah Blah"
        .Attachments.Add (Fname)
        .Sensitivity = 2


        ' You can add other files by uncommenting the following line.
        '.Attachments.Add ("C:\test.txt")
        ' In place of the following statement, you can use ".Display" to
        ' display the mail.
        .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

ActiveWorkbook.Close savechanges:=True
'Excel.Application.Quit
'Outlook.Application.Quit
End Sub

Open in new window


What content are you looking to send?
Can you attach your Excel worksheet you are working with?
1
 

Author Comment

by:Sam Coombes
ID: 41837719
Thank you so much. I just need to send a couple of the cells of information. Which part of your code specifies which mailbox to use. As my outlook has 3 different email accounts and I need to select an account which email should be sent from.
0
 
LVL 1

Expert Comment

by:Andrew White
ID: 41837907
See Ron De Bruin's explanation of SendUsingAccount at http://www.rondebruin.nl/win/s1/outlook/account.htm
0
 
LVL 22

Expert Comment

by:yo_bee
ID: 41840092
Can you attach a sample of your excel worksheet so I can rework the code to meet your needs?
0
 

Author Closing Comment

by:Sam Coombes
ID: 41844724
I don't need the attachment bit but I will work on changing that thank u very much.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now