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
On Error Goto Err_Mail
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
Cancel = True
Else
Cancel = False
Exit Sub
End If
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 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
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
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
End If
If Not oOutlookApp Is Nothing Then
Set oOutlookMail = oOutlookApp.CreateItem(0)
If Not oOutlookMail Is Nothing Then
With oOutlookMail
.To = sTo
.Subject = "Ref: " & sName & " Action: " & sAction
.Body = sBody & vbCr & sDetails
.Importance = iImportance
.ReadReceiptRequested = bReceipt
.Display
CreateMailItem = True
End With
End If
End If
Set oOutlookMail = Nothing
Set oOutlookApp = Nothing
End Function
|