Outlook doesn't seem to delete appointments completely

B

Ben

Hello,

I have the following problem:
The initial situation: A program I made can export appointments to
Outlook via the OLE interface. The exported appointments get an
addtional property so that I can distinguish the user-created
appointments from the exported ones.
Through my program the user can also delete all the previously exported
appointments.
This does seem to work, i.e. the appointments don't show up anymore in
Outlook, but, and that's the problem, in the calender the days with
appointments are still shown bold, and if I try to create a new
appointment that overlaps with the previous appointment (that should
now be deleted) I get the message that the new appointment overlaps
with an existing one - the previous one that somehow didn't get deleted
completely.

To get the appointments that should be removed I do an Items.Restrict
(where Items is the list of all appointments) with the appropriate
filter expression. Then I simply do a for loop and delete the item at
index 1 (with RestrictedItems.Remove(1)) until all items are deleted.

I don't know if it's a problem with my code or with Outlook as my code
isn't particularly complicated.
 
G

Geoff

Ben:
RestrictedItems.Remove(1)

The above code line is the problem. You've removed the Appointment Item from
the restricted items collection but, unfortunately, Outlook does not refresh
its Date Navigation pane as well.

The solution is to point an AppointmentItem object to each item in the
restricted collection and delete the AppointmentItem object. Then Outlook
will refresh its Date Navigation pane.

Below is the code I wrote in Microsoft Access to track down your problem.
Notice the call to "DeleteNextItem" works and the call to
"RemoveItemFromFilteredCollection" doesn't. (You could copy and paste into
an Access module to see it work.)

Regards
Geoff


Option Compare Database
Option Explicit

Private mobjOLA As Outlook.Application
Private mobjAPPT As Outlook.AppointmentItem
Private mobjNS As Outlook.NameSpace
Private mobjFLDR As Outlook.MAPIFolder
Private mobjITEMS As Outlook.Items
Private mobjITEMS_FILTERED As Outlook.Items
Private mobjOLItem As Object


Private Sub DeleteOutlookAppointmentUsingRestrictOnUserProperty()

' This subprocedure creates Appointment Items
' with a UserProperty.
' This subprocedure then deletes those
' Appointment Items using the UserProperty.

' Initialise Outlook:
Set mobjOLA = CreateObject("Outlook.Application")
Set mobjNS = mobjOLA.GetNamespace("MAPI")
mobjNS.Logon , , False, False

' Create two AppointmentItems with a
' UserProperty:
Call CreateAppointments

' Go to Outlook and verify Appointment
' Items have been created, then continue:
Stop

' Count Items in Calendar Folder:
Set mobjFLDR = mobjNS.GetDefaultFolder(olFolderCalendar)
Set mobjITEMS = mobjFLDR.Items
Debug.Print "Count of Items in Calendar Folder = " _
& mobjITEMS.Count

' Delete Items:
Call DeleteAppointmentsUsingRestrict
Call CleanUp

Bye:

Exit Sub

End Sub

Private Sub CreateAppointments()

' Creates some Appointment Items
' in Outlook's Calendar.

Dim datAppt As Date
Dim I As Integer

datAppt = DateSerial(2006, 12, 17) + TimeSerial(18, 0, 0)
For I = 1 To 5
GoSub AddNextAppointment
Next

Bye:

Exit Sub

AddNextAppointment:

Set mobjAPPT = mobjOLA.CreateItem(olAppointmentItem)
mobjAPPT.Subject = "Test Appointment " & CStr(I)
mobjAPPT.UserProperties.Add "BensAppointment", olText
mobjAPPT.UserProperties("BensAppointment") _
= "BenCreatedThisAppointment"

datAppt = DateAdd("n", 30, datAppt)
mobjAPPT.Start = datAppt
mobjAPPT.Save
Return

End Sub

Private Sub DeleteAppointmentsUsingRestrict()

' Use a FOR-NEXT loop to delete
' items in a restricted collection.

Dim I As Integer
Dim J As Integer

' Get restricted collection:
Set mobjITEMS_FILTERED = mobjITEMS.Restrict _
("[BensAppointment] = ""BenCreatedThisAppointment""")

' Count items in restricted collection:
Debug.Print "Count of Items in Filtered Collection = " _
& mobjITEMS_FILTERED.Count

' Delete items in restricted collection:
For I = mobjITEMS_FILTERED.Count To 1 Step -1
GoSub DeleteNextItem
' The following remmed out code line won't work
' (see notes below)
'GoSub RemoveItemFromFilteredCollection
Next

Bye:

Exit Sub

DeleteNextItem:

' THIS WORKS!
' The Appointment Item is deleted and
' the Date Navigator Pane is refreshed.
J = J + 1
Debug.Print "Loop " & J
Set mobjOLItem = mobjITEMS_FILTERED.Item(I)
If TypeName(mobjOLItem) <> "AppointmentItem" Then Return
Set mobjAPPT = mobjOLItem
Debug.Print " " & mobjAPPT.Subject
mobjAPPT.Delete
Return

RemoveItemFromFilteredCollection:

' THIS DOES NOT WORK!
' The Appointment Item is removed from the
' Restricted Collection, but Outlook does
' not refresh its Date Navigator pane.
J = J + 1
Debug.Print "Loop " & J
mobjITEMS_FILTERED.Remove 1

Return

End Sub

Private Sub CleanUp()

Set mobjAPPT = Nothing
Set mobjOLItem = Nothing
Set mobjITEMS_FILTERED = Nothing
Set mobjITEMS = Nothing
Set mobjFLDR = Nothing
Set mobjNS = Nothing
Set mobjOLA = Nothing

End Sub
 
G

Geoff

Ben:

Apologies. The previous post was a working draft-in-progress. I meant to
save it to my Drafts folder.

However, I think the previous post is probably right insofar as it goes, but
I've since discovered more information. I'm trying to get to the bottom of
the contention that you have to set a UserProperty on a (dummy) Appointment
Item *after* the item has been saved to the Calendar folder in order to
create a User-Defined field on the folder. Apparently you have to be careful
to do it this way round, rather than set a UserProperty on an Appointment
Item and then save it to the folder. It seems it is essential to create the
User-Defined field on the folder before you can use the field in a filter
expression used with FIND or RESTRICT.

At this stage, I would say it seems it would be a whole lot easier if you
could add to the Fields collection in a more direct way, eg with an
Fields.Add or Fields.CreateField method. Also, the fact that UserProperties
and Fields are used in this way means you have to be careful how you read
various help systems, textbooks, etc. I'll post back what I turn up (if
anything) as I'm intrigued with what's going on here.

Have you made any progress since your original post?

Geoff




Geoff said:
Ben:
RestrictedItems.Remove(1)

The above code line is the problem. You've removed the Appointment Item
from the restricted items collection but, unfortunately, Outlook does not
refresh its Date Navigation pane as well.

The solution is to point an AppointmentItem object to each item in the
restricted collection and delete the AppointmentItem object. Then Outlook
will refresh its Date Navigation pane.

Below is the code I wrote in Microsoft Access to track down your problem.
Notice the call to "DeleteNextItem" works and the call to
"RemoveItemFromFilteredCollection" doesn't. (You could copy and paste into
an Access module to see it work.)

Regards
Geoff


Option Compare Database
Option Explicit

Private mobjOLA As Outlook.Application
Private mobjAPPT As Outlook.AppointmentItem
Private mobjNS As Outlook.NameSpace
Private mobjFLDR As Outlook.MAPIFolder
Private mobjITEMS As Outlook.Items
Private mobjITEMS_FILTERED As Outlook.Items
Private mobjOLItem As Object


Private Sub DeleteOutlookAppointmentUsingRestrictOnUserProperty()

' This subprocedure creates Appointment Items
' with a UserProperty.
' This subprocedure then deletes those
' Appointment Items using the UserProperty.

' Initialise Outlook:
Set mobjOLA = CreateObject("Outlook.Application")
Set mobjNS = mobjOLA.GetNamespace("MAPI")
mobjNS.Logon , , False, False

' Create two AppointmentItems with a
' UserProperty:
Call CreateAppointments

' Go to Outlook and verify Appointment
' Items have been created, then continue:
Stop

' Count Items in Calendar Folder:
Set mobjFLDR = mobjNS.GetDefaultFolder(olFolderCalendar)
Set mobjITEMS = mobjFLDR.Items
Debug.Print "Count of Items in Calendar Folder = " _
& mobjITEMS.Count

' Delete Items:
Call DeleteAppointmentsUsingRestrict
Call CleanUp

Bye:

Exit Sub

End Sub

Private Sub CreateAppointments()

' Creates some Appointment Items
' in Outlook's Calendar.

Dim datAppt As Date
Dim I As Integer

datAppt = DateSerial(2006, 12, 17) + TimeSerial(18, 0, 0)
For I = 1 To 5
GoSub AddNextAppointment
Next

Bye:

Exit Sub

AddNextAppointment:

Set mobjAPPT = mobjOLA.CreateItem(olAppointmentItem)
mobjAPPT.Subject = "Test Appointment " & CStr(I)
mobjAPPT.UserProperties.Add "BensAppointment", olText
mobjAPPT.UserProperties("BensAppointment") _
= "BenCreatedThisAppointment"

datAppt = DateAdd("n", 30, datAppt)
mobjAPPT.Start = datAppt
mobjAPPT.Save
Return

End Sub

Private Sub DeleteAppointmentsUsingRestrict()

' Use a FOR-NEXT loop to delete
' items in a restricted collection.

Dim I As Integer
Dim J As Integer

' Get restricted collection:
Set mobjITEMS_FILTERED = mobjITEMS.Restrict _
("[BensAppointment] = ""BenCreatedThisAppointment""")

' Count items in restricted collection:
Debug.Print "Count of Items in Filtered Collection = " _
& mobjITEMS_FILTERED.Count

' Delete items in restricted collection:
For I = mobjITEMS_FILTERED.Count To 1 Step -1
GoSub DeleteNextItem
' The following remmed out code line won't work
' (see notes below)
'GoSub RemoveItemFromFilteredCollection
Next

Bye:

Exit Sub

DeleteNextItem:

' THIS WORKS!
' The Appointment Item is deleted and
' the Date Navigator Pane is refreshed.
J = J + 1
Debug.Print "Loop " & J
Set mobjOLItem = mobjITEMS_FILTERED.Item(I)
If TypeName(mobjOLItem) <> "AppointmentItem" Then Return
Set mobjAPPT = mobjOLItem
Debug.Print " " & mobjAPPT.Subject
mobjAPPT.Delete
Return

RemoveItemFromFilteredCollection:

' THIS DOES NOT WORK!
' The Appointment Item is removed from the
' Restricted Collection, but Outlook does
' not refresh its Date Navigator pane.
J = J + 1
Debug.Print "Loop " & J
mobjITEMS_FILTERED.Remove 1

Return

End Sub

Private Sub CleanUp()

Set mobjAPPT = Nothing
Set mobjOLItem = Nothing
Set mobjITEMS_FILTERED = Nothing
Set mobjITEMS = Nothing
Set mobjFLDR = Nothing
Set mobjNS = Nothing
Set mobjOLA = Nothing

End Sub
 
S

Sue Mosher [MVP-Outlook]

At this stage, I would say it seems it would be a whole lot easier if you
could add to the Fields collection in a more direct way, eg with an
Fields.Add or Fields.CreateField method.

Outlook 2007 adds that functionality

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Geoff

Sue:
Outlook 2007 adds that functionality

Thank you.
And many thanks to you and Ken Slovak for all your hard work and help to me
and the newsgroup community over the past year. I think your new website
is stunning.
Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top