Creating an email from data from a sheet

jugger0s

New Member
Joined
Nov 4, 2014
Messages
4
Excel version - 2013

Hello, I've been trying to figure this out for a while with no joy.

data example
VenueProviderDateTimeEnrolments RegisterLesson Plan
E2Alpha14/11/201415:0044 ReceivedReceived
E4Alpha16/11/201416:0015 ReceivedReceived
E5Bravo04/09/201417:00 ReceivedReceived
E3Bravo06/03/201418:0043Received
E3Charlie04/02/201519:002 ReceivedReceived

<colgroup><col span="2"><col><col span="3"><col></colgroup><tbody>
</tbody>

Ok, What I'm trying to do is for each row i would like to make a button or selection somewhere that will select the row and create an email with that rows data. For example A button or macro in the venue column that will open an email, and add the enrolments, register and lesson plan information from that row. I've done something like that in Access for a database form but it doesn't translate.

This is what I would like to see in the email,

Enrolments Register Lesson Plan
44 Received Received

Then once this is working i'll then tackle the tricky task of defining the blanks to show what is required from who i send the email to... but thats part two... :)

If anyone had any idea it will be greatly appreciated.

Thanks for your time,

Gareth.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Create a macros using Relative References and saved into your personal macros, select a row with data, copy, create new tab, rename tab, paste contents into new tab, FILE>Save and send, pick the format and save.

Now every time you need to send a diferent row, you just have to select the row and run the macros.
 
Upvote 0
Hi and welcome to the MrExcel Message Board.

I use something like this:
Code:
' Tick: Tools-->References: Microsoft Outlook 15.0 Object Library
Sub CreateEmail(ThisRow As Long)

    '====================================================================================
    ' Get Data from Excel
    '====================================================================================
    Dim Enrolments As String
    Dim Register As String
    Dim LessonPlan As String
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    Enrolments = ws.Range("E" & ThisRow).Value
    Register = ws.Range("F" & ThisRow).Value
    LessonPlan = ws.Range("G" & ThisRow).Value
    
    '====================================================================================
    ' Create Email
    '====================================================================================
    Dim oOlApp As Outlook.Application
    On Error Resume Next
    Set oOlApp = GetObject(, "Outlook.Application") ' Outlook needs to be already open
    If Err.Number > 0 Then ' If not open then open Outlook
        On Error GoTo 0
        Err.Number = 0
        Set oOlApp = CreateObject("Outlook.Application")
    End If

    Dim oOLMail As MailItem
    Set oOLMail = oOlApp.CreateItem(0)
    Dim strBody As String
    Dim tmpBody As String
    
    strBody = "Hi, <br>" & _
              "<br> Enrolments: " & Enrolments & "<br>" & _
              "<br> Register: " & Register & "<br>" & _
              "<br> LessonPlan: " & LessonPlan & "<br>" & _
              "<br>Regards,"
         
    With oOLMail
        .Display (False)
        tmpBody = .HTMLBody ' Store the sig to re-add it later
        .To = "Someone.Else@xyz.com"
        .CC = "Support Inbox"
        .BCC = ""
        .Subject = "Enrolments request: " & Enrolments
        .BodyFormat = olFormatHTML
        .HTMLBody = "<span style=""font-family: HPFutura Book; font-size: 13pt;"">" & strBody & "<br>" & tmpBody & "</span>"
        .Display (True)
    End With
    
    ' Leave Outlook open so that the email can be sent
    
End Sub

That is a macro that resides in a Module and needs to be given the row number that you want to process.
Note that the Microsoft Outlook 15.0 Object Library is required.

Instead of using buttons I prefer to right-click a cell. I picked column A for this example. The calling program can then go behind the worksheet:
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Application.EnableEvents = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    If Target.Count = 1 And Target.Value <> "" Then ' Only one, non-blank cell selected?
        If Not Intersect(Target, ws.Columns(1)) Is Nothing Then  ' Selection in column 1?
            Call CreateEmail(Target.Row)
            Cancel = True  ' Switch off the right-click menu
        End If
    End If
    Application.EnableEvents = True
End Sub

With both those macros in place you can right-click somewhere in column A and an email will be created (assuming you use Outlook like I do!)

Obviously, you will need to modify strBody, Subject addressees etc.
Note the trick to add the sig.
 
Upvote 0
That first block of code should have looked like this:
Code:
' Tick: Tools-->References: Microsoft Outlook 15.0 Object Library
Sub CreateEmail(ThisRow As Long)

    '====================================================================================
    ' Get Data from Excel
    '====================================================================================
    Dim Enrolments As String
    Dim Register As String
    Dim LessonPlan As String
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    Enrolments = ws.Range("E" & ThisRow).Value
    Register = ws.Range("F" & ThisRow).Value
    LessonPlan = ws.Range("G" & ThisRow).Value
    
    '====================================================================================
    ' Create Email
    '====================================================================================
    Dim oOlApp As Outlook.Application
    On Error Resume Next
    Set oOlApp = GetObject(, "Outlook.Application") ' Outlook needs to be already open
    If Err.Number > 0 Then ' If not open then open Outlook
        On Error GoTo 0
        Err.Number = 0
        Set oOlApp = CreateObject("Outlook.Application")
    End If

    Dim oOLMail As MailItem
    Set oOLMail = oOlApp.CreateItem(0)
    Dim strBody As String
    Dim tmpBody As String
    <pre>
    strBody = "Hi, <br>" & _
              "<br> Enrolments: " & Enrolments & "<br>" & _
              "<br> Register: " & Register & "<br>" & _
              "<br> LessonPlan: " & LessonPlan & "<br>" & _
              "<br>Regards,"
    </pre>
    With oOLMail
        .Display (False)
        tmpBody = .HTMLBody ' Store the sig to re-add it later
        .To = "Someone.Else@xyz.com"
        .CC = "Support Inbox"
        .BCC = ""
        .Subject = "Enrolments request: " & Enrolments
        .BodyFormat = olFormatHTML
<pre>
        .HTMLBody = "<span style=""font-family: HPFutura Book; font-size: 13pt;"">" & strBody & "<br>" & tmpBody & "</span>"
</pre>
        .Display (True)
    End With
    
    ' Leave Outlook open so that the email can be sent
    
End Sub


My apologies.

I will get the hang of this inter-web stuff eventually!
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top