Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, March 29, 2011 6:33 PM
I am searching for someone who can either post some code or point me in the right direction on how to search thru Calendar items in an Outlook Calendar and then delete that item. I have found code on how to add a new item but not on how to find and delete an item.
I would really appreciate any help you can provide. Below is the code I use to add a Calendar item.
Set olapp = CreateObject("Outlook.Application")
Set olNs = olapp.GetNamespace("MAPI")
Set oFolder = olNs.GetDefaultFolder(olFolderCalendar) 'olFolderCalendar)
Set oItem = olapp.CreateItem(olAppointmentItem)
' Set some common properties.
oItem.start = OutlookStart
oItem.End = OutlookEnd
oItem.Duration = OutlookDuration
oItem.Subject = OutlookSubject
oItem.Body = Me.tb_comments
' Save to Calendar.
oItem.Save
' Clean up.
olapp = Nothing
oItem = Nothing
PietroA
All replies (7)
Wednesday, March 30, 2011 12:12 AM âś…Answered | 3 votes
I finally got it working. Thanks to Andrey for pointing me in the right direction. I am posting the solution here so others can find it.
Dim olApp As Outlook.Application
Dim objAppointment As Outlook.AppointmentItem
Dim objAppointments As Outlook.MAPIFolder
Dim objNameSpace As Outlook.NameSpace
Dim objProperty As Outlook.UserProperty
Dim OutlookStartTime, OutlookEndTime As Date
Dim sFilter As Variant
OutlookStartTime = CDate(AP_Date & " " & AP_Start_Time)
OutlookEndTime = CDate(AP_Date & " " & AP_End_Time)
Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objAppointments = objNameSpace.GetDefaultFolder(olFolderCalendar)
sFilter = "[Start] = '" & Format(OutlookStartTime, "ddddd h:nn AMPM") & _
"' And [End] = '" & Format(OutlookEndTime, "ddddd h:nn AMPM") & "' " & _
" And [Subject] = '" & Me.AP_With_Whom & " - " & Me.AP_Type & "'"
Set objAppointment = objAppointments.items.Find(sFilter)
If Not TypeName(objAppointment) = "Nothing" Then
objAppointment.Delete
End If
Set objAppointment = Nothing
Set objAppointments = Nothing
End Sub
PietroA
Tuesday, March 29, 2011 7:33 PM
Hi,
here is a good example of how to find a particular Contact. I hope there will be a little you have to modify for making ot work with Appointments.
http://msdn.microsoft.com/en-us/library/aa220093(v=office.11).aspx
At the beginning of the article you can also find a wide explanation of how .Find method works.
Andrey V Artemyev | Saint-Petersburg, Russia
Tuesday, March 29, 2011 7:58 PM
Thanks for the help Andrey but I was hoping for an example using a Calendar item where I could see how to use dates and times. The code works beautifully for finding contacts and I do appreciate the effort.PietroA
Tuesday, March 29, 2011 8:28 PM
Did you try smth like this?
Set oItem = oFolder.Items.Find("[Start] = '" Format(OutlookStart, "ddddd h:nn AMPM") "' and [End] = '" Format(OutlookEnd, "ddddd h:nn AMPM")) & "'"
Andrey V Artemyev | Saint-Petersburg, Russia
Tuesday, March 29, 2011 8:49 PM
I kept getting the ' and "" confused. The format of the statement is very confusing. I tried your example above and got an error: [Compile error: Expected: list separator or )] and then if highlights for FORMAT portion of the statement.
I really appreciate your time.
Below is the code I have so far:
Dim olApp As Outlook.Application
Dim objAppointment As Outlook.AppointmentItem
Dim objAppointments As Outlook.MAPIFolder
Dim objNameSpace As Outlook.NameSpace
Dim objProperty As Outlook.UserProperty
Dim OutlookStartTime, OutlookEndTime As Date
OutlookStartTime = #3/24/2011 2:00:00 PM#
OutlookEndTime = #3/24/2011 2:30:00 PM#
Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objAppointments = objNameSpace.GetDefaultFolder(olFolderCalendar)
Set objAppointment = objAppointments.items.Find("[Start] = '" Format(OutlookStartTime, "m/d/yyyy h:nn AMPM") "' and [End] = '" Format(OutlookEndTime, "m/d/yyyy h:nn AMPM")) & "'"
PietroA
Tuesday, March 29, 2011 9:22 PM
Sorry, there is 1 extra bracket. The correct code is
Set objAppointment = objAppointments.items.Find("[Start] = '" Format(OutlookStartTime, "m/d/yyyy h:nn AMPM") "' and [End] = '" Format(OutlookEndTime, "m/d/yyyy h:nn AMPM") & "'"
I get this code from the article I provided above. Unfortunately, I have no Outlook installed and can't try to test these attempts. Maybe somebody here has an appropriate experience.
Andrey V Artemyev | Saint-Petersburg, Russia
Tuesday, March 29, 2011 9:32 PM
I fixed the statement and now I get an error message saying that there is a type mismatch in the field OutlookStart. Below is the code I'm now using.
Set objAppointment = objAppointments.items.Find("[Start] = #" & Format(OutlookStartTime, "m/d/yyyy h:nn AMPM") & "# and [End] = #" & Format(OutlookEndTime, "m/d/yyyy h:nn AMPM") & "#")
PietroA