Delete an All Day Appointment using VBA

A

Andy

I have successfully added an All Day appointment for tomorrow in Outlook from
Access using :-

--------------

Dim objOutlook As Outlook.Application
Dim objCalendar As Object
Dim objAppointment As Object

Set objOutlook = New Outlook.Application
Set objCalendar = objOutlook.Session.GetDefaultFolder(olFolderCalendar)

Set objAppointment = objCalendar.Items.Add
With objAppointment
.Subject = "Test"
.Start = Date + 1
.AllDayEvent = True
End With
objAppointment.Close olSave
Set objAppointment = Nothing
Set objOutlook = Nothing
 
S

Sue Mosher [MVP-Outlook]

Delete it in what context? If you get the item's EntryID after saving it and
store that in your Access database, you can return the same item with the
Namespace.GetItemFromID and then delete it with the AppointmentItem.Delete
method.
 
A

Andy

Thanks Sue - good idea.

I'm OK with obtaining and storing the EntryID for later use but could you
help more with the syntax with Namespace.GetItemFromID and
AppointmentItem.Delete - I'm not so familar with these two contructs.

Regards - Andy.
 
S

Sue Mosher [MVP-Outlook]

When in doubt, check the object browser: Press ALt+F11 to open the VBA
environment in Outlook, then press F2. Switch from <All Libraries> to
Outlook to browse all Outlook objects and their properties, methods, and
events. Select any object or member, then press F1 to see its Help topic.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
A

Andy

Sue - Sorry to push a bit more on this subject but it is interesting.
Firstly, I took your advice and looked at the VBA help files to work out the
syntax of the various methods. I did struggle with Namespace though and found
that I had to use Session instead.

Anyway - I created 3 Appointment items one after the other and saved each
before obtaining their EntryID - suprisingly each EntryID was the same even
though the 3 appointments were on different days albeit with the same Subject
for each.

I stored these away but when I came to later use them to delete the
appointments, I got a failure when I tried to set a variable as follows :-

CalEntryIDR = objOutlook.Session.GetItemFromID(CalEntryID) ' CalEntryID is
the long EntryID value

Also when I actually got this statement to work (just after creating the
appointment) the returned value was the Subject of the appointment ???

Any ideas? Especially the difference between Namespace and Session and why
the 3 IDs were the same above.

Thanks.

The variable CalEntryID contains the long EntryID value.
 
S

Sue Mosher [MVP-Outlook]

Show the code used to instantiate the Namespace and to save the items and
get their EntryIDs.

GetItemFromID returns an object. You need to use Set:

Set CalEntryIDR = objNS.GetItemFromID(CalEntryID)

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
A

Andy

Thanks Sue - Here is cut down code as requested [ it does the add's OK but
not the delete's - fails on Set objAppointment =
objOutlook.Session.GetItemFromID(CalEntryID) ] :-

Dim objOutlook As Outlook.Application
Dim objCalendar As Object
Dim objAppointment As Object
Dim CalEntryID as Variant
Dim CalEntryIDR as Object
Dim SQLcmd as String

Set objOutlook = New Outlook.Application
Set objCalendar = objOutlook.Session.GetDefaultFolder(olFolderCalendar)

Select Case Action

Case "A"

cc = 0

Do While cc <= 2

Set objAppointment = objCalendar.Items.Add
With objAppointment
.Subject = "Test"
.Start = Date + cc
.AllDayEvent = True
End With

objAppointment.Close olSave

CalEntryID = objAppointment.EntryID

SQLcmd = "Insert into table tblx (EN,EI) values ('" & cc & "', '" &
CalEntryID & "')"
Docmd.RunSQL SQLcmd

cc = cc + 1

Loop

Case "D"

cc = 0

Do While cc <= 2

CalEntryID = DLookup("[EI]", "tblx", "[EN] = " & cc)
Set objAppointment = objOutlook.Session.GetItemFromID(CalEntryID)
objAppointment.Delete

cc = cc + 1

Loop

End Select

Set objAppointment = Nothing
Set objOutlook = Nothing
 
A

Andy

I eventually figured out why the deletes weren't working.

Below, when I stored the EntryID away in the database I hadn't realised that
the default size of a text field in Access is 50 characters. The EntryID is
much more than this (about 160 chrs) so the ID's were being truncated.
By increasing the size of the text field the ID's were stored correctly so
when they were retieved in order to delete the appointment at a later date,
it worked fine.
So the code was OK.

Andy.


-----------
Andy said:
Thanks Sue - Here is cut down code as requested [ it does the add's OK but
not the delete's - fails on Set objAppointment =
objOutlook.Session.GetItemFromID(CalEntryID) ] :-

Dim objOutlook As Outlook.Application
Dim objCalendar As Object
Dim objAppointment As Object
Dim CalEntryID as Variant
Dim CalEntryIDR as Object
Dim SQLcmd as String

Set objOutlook = New Outlook.Application
Set objCalendar = objOutlook.Session.GetDefaultFolder(olFolderCalendar)

Select Case Action

Case "A"

cc = 0

Do While cc <= 2

Set objAppointment = objCalendar.Items.Add
With objAppointment
.Subject = "Test"
.Start = Date + cc
.AllDayEvent = True
End With

objAppointment.Close olSave

CalEntryID = objAppointment.EntryID

SQLcmd = "Insert into table tblx (EN,EI) values ('" & cc & "', '" &
CalEntryID & "')"
Docmd.RunSQL SQLcmd

cc = cc + 1

Loop

Case "D"

cc = 0

Do While cc <= 2

CalEntryID = DLookup("[EI]", "tblx", "[EN] = " & cc)
Set objAppointment = objOutlook.Session.GetItemFromID(CalEntryID)
objAppointment.Delete

cc = cc + 1

Loop

End Select

Set objAppointment = Nothing
Set objOutlook = Nothing


---------

Sue Mosher said:
Show the code used to instantiate the Namespace and to save the items and
get their EntryIDs.

GetItemFromID returns an object. You need to use Set:

Set CalEntryIDR = objNS.GetItemFromID(CalEntryID)

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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