Private Sub Command89_Click()
Call subDeleteAutoGereratedAppointment(#2/16/2025 12:00:01 AM#, #2/16/2025 11:59:59 PM#, "user1@kosmosbusiness.onmicrosoft.com")
End Sub
Sub subDeleteAutoGereratedAppointment(dtmStart As Date, dtmEnd As Date, strOutlookEmail As String)
Dim olApp As Object
Dim nsMAPI As Object
Dim olAppointments As Object
Dim olFilterAppointments As Object
Dim olAppointmentItem As Object
Dim blnIsOutlookRunning As Boolean
Dim strDateRange As String
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Err.Clear
Set olApp = CreateObject("Outlook.Application")
blnIsOutlookRunning = False
Else
blnIsOutlookRunning = True
End If
On Error GoTo Error_Handler
DoEvents
Set nsMAPI = olApp.GetNamespace("MAPI")
Set olAppointments = nsMAPI.Folders(strOutlookEmail).Folders("Calendar")
strDateRange = "[Start] >= '" & _
Format$(dtmStart, "mm/dd/yyyy hh:mm AMPM") _
& "' AND [Start] <= '" & _
Format$(dtmEnd, "mm/dd/yyyy hh:mm AMPM") & "'"
Set olFilterAppointments = olAppointments.Items.Restrict(strDateRange)
Debug.Print olFilterAppointments.Count & " appointments found."
'Iterate through each appt in our calendar
For Each olAppointmentItem In olFilterAppointments
Debug.Print olAppointmentItem.Subject, olAppointmentItem.Location
If olAppointmentItem.Location = "123" Then
olAppointmentItem.Delete
End If
Next
If blnIsOutlookRunning = False Then 'Since we started Outlook, we should close it now that we're done
olApp.Quit 'There seems to be a delay in this action taking place, but does eventually take place
End If
Error_Handler_Exit:
On Error Resume Next
Set olAppointmentItem = Nothing
Set olFilterAppointments = Nothing
Set olAppointments = Nothing
Set nsMAPI = Nothing
Set olApp = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: GetFutureOutlookEvents" & vbCrLf & _
"Error Description: " & Err.Description _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub