Good afternoon! I have cobbled together some code using what has been available (I don't take ownership of any of it), but I'm having difficulty getting it to delete a calendar appointment.
I'm tracking due dates, and I am able to create the appointments in outlook, via excel. I'm having difficulty in writing code that will delete the created calendar item if a deadline is met.
Any suggestions?
Dim row As Integer
Dim appointment As Boolean
Public Sub CreateBankStatementCalendar()
row = Selection.row
appointment = AddToCalendar(Cells(row, 7), "Bank Statement Due for " & ThisWorkbook.Sheets("Sheet1").Range("A1"), "", #12:00:00 PM#, #12:30:00 PM#)
'appointment = AddToCalendar(Cells(row, 8), "1st Files to Send", "Office2", #8:00:00 AM#, #9:00:00 AM#)
'If IsDate(Cells(row, 28)) Then appointment = AddToCalendar(Cells(row, 9), "Arrange Monitors", "Office3", #9:00:00 AM#, #10:00:00 AM#)
'If IsDate(Cells(row, 29)) Then appointment = AddToCalendar(Cells(row, 8), "Confirm Monitors", "Office4", #9:00:00 AM#, #10:00:00 AM#)
'If IsDate(Cells(row, 29)) Then appointment = AddToCalendar(Cells(row, 9), "DDS", "Office5", #10:00:00 AM#, #11:00:00 AM#)
'If Cells(row, 20) > 0 Then appointment = AddToCalendar(Cells(row, 21), "3rd Party letters to Send", "Office6", #10:00:00 AM#, #11:00:00 AM#)
End Sub
Function AddToCalendar(dteDate As Date, strSubject As String, strLoc As String, dteStart As Date, dteEnd As Date) As Boolean
On Error GoTo ErrorHandler
'--------------------------------------------------
'CREATE AND SET VARIABLES
Dim olApp As Object
Dim objNewAppt As Object
Set olApp = GetOutlookApplication
If olApp Is Nothing Then
MsgBox "Cannot access Outlook. Exiting now", vbInformation
GoTo ErrorHandler
End If
'--------------------------------------------------
'CREATE APPOINTMENT
Set objNewAppt = olApp.createitem(1) ' 1 is the constant for olAppointmentItem when using late-bound code, or VBScript
body = "Deadline to turn in bank statement for " & ThisWorkbook.Sheets("Sheet1").Range("A1") & Chr(13) & "Bank Statement dates: " & Cells(row, 2) & _
Chr(13) & "Due back to Program Manager by " & Cells(row, 7) 'body of message
With objNewAppt
.meetingstatus = 1
.Start = dteDate & " " & dteStart
.End = dteDate & " " & dteEnd
.Subject = strSubject
.Location = strLoc
.body = body
.reminderset = True
.ReminderMinutesBeforeStart = 30
.Recipients.Add ThisWorkbook.Sheets("Email Addresses").Range("a2")
.Recipients.Add ThisWorkbook.Sheets("Email Addresses").Range("a3")
.Recipients.Add ThisWorkbook.Sheets("Email Addresses").Range("a4")
.Recipients.resolveall
.Save
.send
End With
AddToCalendar = True
GoTo ExitProc
ErrorHandler:
AddToCalendar = False
MsgBox "Please make sure that the dates are in the correct format and" & Chr(13) & _
"you have selected a cell in the project row you wish to create tasks for."
ExitProc:
Set olApp = Nothing
Set objNewAppt = Nothing
End Function
Function GetOutlookApplication()
On Error Resume Next
Set GetOutlookApplication = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set GetOutlookApplication = CreateObject("Outlook.Application")
End If
On Error GoTo 0
End Function