Sub SendEmail()
Dim olkApp As Object
Dim MItem As Object
Dim olkAppointment As Object
Dim cell As Range
Dim Subj As String
Dim EmailAddr As String
Dim Adate As Date
Dim Atime As String
Dim Msg As String
Dim Msg2 As String
Dim SendStatus As String
Dim FileLocation As String
Dim strApptBody As String
Const olAppointmentItem As Integer = 1
Const olBusy As Integer = 2
Const olICal As Integer = 8
Const olMailItem As Integer = 0
'Loop through the rows
For Each cell In Columns("H").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "*@*" Then
'Get the data
SendStatus = cell.Offset(0, 1)
If SendStatus <> "To be sent" Then GoTo NotThisOne
Subj = cell.Offset(0, -1).Value
EmailAddr = cell.Value
Adate = cell.Offset(0, -6).Value
Atime = cell.Offset(0, -5).Value
FileLocation = "C:\ApptFiles\OutlookAppointment.ics"
'FileLocation = Environ("temp") & "\" & "OutlookAppointment.ics"
cell.Offset(0, 1).Value = "Sent"
Set olkApp = CreateObject("outlook.application")
Set olkAppointment = olkApp.CreateItem(olAppointmentItem)
Msg2 = "New Task"
With olkAppointment
.Start = Adate
.End = .Start + TimeValue("00:30:00")
.Subject = Msg2
.Location = " "
.Body = Msg
.BusyStatus = olBusy
.ReminderMinutesBeforeStart = 2880
.ReminderSet = True
'.Display
'Save the iCalendar file in a known folder
.SaveAs FileLocation, olICal
'Use Close to retain the new appointment within the Outlook Calendar, or Delete to delete it.
'Both options keep the just-created .ics file
'.Close False
.Delete
End With
Set MItem = olkApp.CreateItem(olMailItem)
With MItem
.To = EmailAddr
.Subject = Subj
.Body = Msg
.Attachments.Add (FileLocation)
.Send
'.Save 'to Drafts folder
End With
strApptBody = strApptBody & "Item: " & Format(cell.Offset(0, -6).Value + cell.Offset(0, -5).Value, "dd mmm yyyy hh:mm") & " | " & cell.Offset(0, -1).Value & vbCrLf
NotThisOne:
End If
Next
If strApptBody <> "" Then
With olkApp.CreateItem(olAppointmentItem)
.Start = DateAdd("d", 7, Now())
.End = .Start + TimeValue("00:30:00")
.Subject = "Event Record"
.Location = " "
.Body = strApptBody
.BusyStatus = olBusy
.ReminderMinutesBeforeStart = 2880
.ReminderSet = True
.Save
.Close False
End With
End If
Set olkApp = Nothing
Set olkAppointment = Nothing
Set olkApp = Nothing
End Sub
|