Excel

Create different Email Messages based on Contents of a cell

Ease of Use

Easy

Version tested with

2000, 2002, 2003 

Submitted by:

gibbo1715

Description:

Allows the user to double click a name on the sheet in row A and then dependant on the content of another cell on the same row a different email message will be generated. 

Discussion:

An example would be where you have a list of customers in your spreadsheet, a column tells you if they have paid or are overdue to pay for items purchased, click the name and an email will be automatcally generated containing the correct text dependant of whether they have paid or not. 

Code:

instructions for use

			

Option Explicit Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _ Cancel As Boolean) Dim sName As String Dim sAction As String Dim sDetails As String Dim sMeYou As String Dim sTo As String Dim sBody As String Dim bSucces As Boolean 'Error Handling On Error Goto Err_Mail 'Check active column is column 1 (Where you want the macro to be called from) If Target.Column = 1 Then With Target sName = .Value sAction = .Offset(0, 1).Value sDetails = .Offset(0, 2).Value sMeYou = .Offset(0, 4).Value sTo = .Offset(0, 5).Value End With 'Stop update mode in cell Cancel = True Else 'Cancel sub if not column 1 and goto update mode Cancel = False Exit Sub End If 'Set mailoptions depending on value of sMeYou and create email If sMeYou = "With Me" Then bSucces = CreateMailItem(sTo, "This is email 1", sDetails, _ sName, sAction, 2, True) ElseIf sMeYou = "With You" Then bSucces = CreateMailItem(sTo, "This Is email 2", sDetails, _ sName, sAction, 2, True) End If ' If mail was created fillin maildate If bSucces Then Target.Offset(0, 6).Value = Now() Else MsgBox "MailItem could not be created" End If Exit Sub Err_Mail: MsgBox "Sorry there has been an error, please contact an administrator" End Sub 'Function to display email item with various options Public Function CreateMailItem(sTo As String, _ sBody As String, _ sDetails As String, _ sName As String, _ sAction As String, _ iImportance As Integer, _ bReceipt As Boolean) As Boolean Dim oOutlookApp As Object Dim oOutlookMail As Object CreateMailItem = False On Error Resume Next 'Use current Outlook app if running 'After pressing send the mail is send right away Set oOutlookApp = GetObject(, "Outlook.Application") 'Not running create a new instance of Outlook If Err <> 0 Then 'After pressing send the email wil stay in Outbox til you open 'Outlook again and press send receive Set oOutlookApp = CreateObject("Outlook.Application") End If 'Create and show the outlook mail item If Not oOutlookApp Is Nothing Then Set oOutlookMail = oOutlookApp.CreateItem(0) If Not oOutlookMail Is Nothing Then With oOutlookMail 'Mail To .To = sTo 'Subject Text .Subject = "Ref: " & sName & " Action: " & sAction 'Body Text .Body = sBody & vbCr & sDetails 'Importance level .Importance = iImportance 'Receipt yes or no .ReadReceiptRequested = bReceipt 'use .Display to show the mail .Display CreateMailItem = True End With End If End If 'Clean up Set oOutlookMail = Nothing Set oOutlookApp = Nothing End Function

How to use:

  1. Open Microsoft Excel
  2. Copy the code
  3. Press Alt + F11 to open the Visual Basic Editor (VBE)
  4. In the Project Window (Left Top) Select Sheet 1
  5. Paste code into the right pane
  6. Click Tools - References on the Menu
  7. Create a reference to the Microsoft Outlook Object Library
  8. Return to Excel Sheet 1 and put data in the row as follows
  9. A. Name - B. Action - C. Details - D. Other - E. (With Me otr With You) - F. Email address of recipient - G. Leave blank this will have a time date stamp inserted when email created
  10. Now double click on the name in column A - your email will be generated
 

Test the code:

  1. Use the example to see how it is set up, to use this code in your own projects, follow the instructions above
 

Sample File:

Multi Emails.zip 11.09KB 

Approved by mdmackillop


This entry has been viewed 302 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express