You mentioned EntryID below. I originally was trying to use EntryID to
identify the exact appointment to delete, but was having trouble storing
and
retrieving it. I tried storing it a table for each of the record I made
the
appointment for as a text field, but that seems to be too short, so I
changed
it to Memo field, but that doesn't seem to work either. Do you know what
field type this EntryID can be stored in an Access table? And yes I'm
interested in the code without using CDO as you mentioned..
Both EntryID and StoreID are very long strings. It may be that the maximum
limit of 255 characters for a text field would be too small. (I don't know
for sure because I've never counted the number of characters in these
strings.) The memo field type should be OK. My guess would be that the
problem is not with the field type, but something else - perhaps the
StoreID. If you had the same appointment item on different machines, then I
think their EntryIDs and StoreIDs would be different, ie they are unique to
a specific machine. I think the StoreID is a unique string that identifies
the *.pst file on a given machine.
When I looked up my sample code today, I was reminded of yet another issue.
This may not be relevant to you but I mention it anyway because it adds
material information to the code I previously posted. This paragraph
explains this further issue and the demo code below shows how to work around
it. There appears to be a precision issue with the way VBA and Outlook store
times. VBA stores date/time serial numbers as real numbers, with the integer
portion (before the decimal point) representing the date, and the decimal
portion (after the decimal point) representing the time. Take the case of
using VBA's DateSerial() and TimeSerial() functions to create a date/time
for an appointment on, say, March 5, 2007 at 11:30 am and use VBA to create
an Appointment starting at that date/time. If you then get VBA to loop
through all the appointments until it finds an appointment starting at 11:30
on March 5, 2007, it *won't* find it. It seems that the time portion of the
Appointment item may use a different number of decimal places to the
TimeSerial() function. If you repeat this test, putting 11:00 am into the
TimeSerial() function, then VBA *will* find the appointment. Presumably,
this indicates that the degree of precision is immaterial for an 11:00 am
appointment, but is not immaterial for an 11:30 am appointment. The
workaround for this issue is that, when VBA loops through all the
appointments looking for an appointment that starts at a specific time, the
code needs to use a range of times, starting 1 second before, and ending 1
second after, the actual appointment time.
The following demo shows all this in action. CDO isn't used, so the
Date-Navigation pane is kept up-to-date. The Appointment item is deleted
from the Calendar folder and then from the Deleted-Items folder. The code
uses the GetItemFromID method to locate the item in the Deleted-Items
folder.
Regards
Geoff
' OUTLOOK OBJECT VARIABLES:
Private mobjOLA As Outlook.Application
Private mobjNS As Outlook.NameSpace
Private mobjFLDR As Outlook.MAPIFolder
Private mobjAPPT As Outlook.AppointmentItem
Private Sub ExampleCall_PermDelete2()
' DEMO 4
Dim strSubject As String
Dim datApptDateTime As Date
' Assumes there is an appointment with the
' following subject and date/time that needs
' to be deleted:
strSubject = "*** MY TEST APPOINTMENT ***"
datApptDateTime = DateSerial(2007, 3, 5) _
+ TimeSerial(11, 30, 0)
Call PermanentlyDeleteAppointment2(strSubject, datApptDateTime)
End Sub
Private Sub PermanentlyDeleteAppointment2( _
strSubject As String, _
datApptDateTime As Date)
' DEMO 4
'
' Use this demo to permanently-delete an Appointment
' when its subject and start Date/Time are known.
' This demo does not use CDO. This has the advantage
' of keeping Outlook's Date-Navigation Pane up-to-date.
'
' This demo also shows how to overcome (what appears
' to be) a precision issue with the way Outlook stores
' times by using a range 1 second before to 1 second
' after the incoming date/time.
Dim fItemDeleted As Boolean
Dim datRangeStart As Date
Dim datRangeEnd As Date
Dim strEntryID As String
Dim strStoreID As String
' Figure out Date/Time range within which the
' appointment must be (ie 1 second before to
' 1 second after the incoming date/time):
datRangeStart = DateAdd("s", -1, datApptDateTime)
datRangeEnd = DateAdd("s", 1, datApptDateTime)
' Get Outlook:
Set mobjOLA = CreateObject("Outlook.Application")
Set mobjNS = mobjOLA.GetNamespace("MAPI")
mobjNS.Logon , , False, False
Set mobjFLDR = mobjNS.GetDefaultFolder(olFolderCalendar)
strStoreID = mobjFLDR.StoreID
' Initialise "Deleted" flag:
fItemDeleted = False
' Loop through all Appointments:
For Each mobjAPPT In mobjFLDR.Items
If mobjAPPT.Start > datRangeStart And _
mobjAPPT.Start < datRangeEnd And _
mobjAPPT.Subject = strSubject Then
' Item found so delete it and exit loop:
GoSub DeleteAppointmentItem
Exit For
End If
Next
' Show final message if not deleted:
If fItemDeleted = False Then
MsgBox "Cannot find Appointment Item to delete.", _
vbOKOnly + vbExclamation, "Information"
End If
Bye:
Set mobjAPPT = Nothing
Set mobjFLDR = Nothing
Set mobjNS = Nothing
Set mobjOLA = Nothing
Exit Sub
DeleteAppointmentItem:
' Store the Appointment Item's EntryID:
strEntryID = mobjAPPT.EntryID
' Delete the Appointment and send it
' to the Deleted-Items folder:
mobjAPPT.Delete
' Point to the Appointment Item in the
' Deleted-Items folder and delete it:
Set mobjAPPT = mobjNS.GetItemFromID(strEntryID, strStoreID)
mobjAPPT.Delete
' Set "Deleted" flag to True:
fItemDeleted = True
Return
End Sub