Remove an individual occurrence of recurring appointment

G

Guest

Hi i write this:

Sub CleanUp()
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colCal As Outlook.Items
Dim objAppt As Outlook.AppointmentItem

Dim objTsk As Outlook.TaskItem

Set objOL = CreateObject("Outlook.Application")
Set objNS = objOL.GetNamespace("MAPI")
Set colCal = objNS.GetDefaultFolder(olFolderCalendar).Items

On Error Resume Next

For Each objAppt In colCal
If objAppt.Categories = "Work" And DateDiff("d", objAppt.Start,
Now()) > 0 Then
objAppt.Delete
End If
Next

Set objAppt = Nothing
Set objTsk = Nothing
Set colCal = Nothing
End Sub

the problem is if objAppt it's a recurring object the metod Delete remove
all, instead i want to remove only a individual occurrence.
TNX
 
K

Ken Slovak - [MVP - Outlook]

Two things first.

If this is Outlook VBA code then don't create an Outlook.Application object,
use the intrinsic Application object exposed in the Outlook VBA.

Second, when deleting items from a collection never use a For Each loop. By
reducing the size of the collection the internal loop counter is being
messed with, which usually results in deleting only half the items. Use a
count down For loop instead:
Dim i As Long
Dim Count As Long

Count = colCal.Count
For i = Count To 1 Step -1
'etc.
Next

To remove an individual occurrence of a recurring series you must first test
that the item is recurring, get the master appointment, then the recurrence
pattern and traverse the pattern to find the occurrence you want:

If objAppt.IsRecurring Then
Dim oRecur As Outlook.RecurrencePattern
Set oRecur = objAppt.GetRecurrencePattern()

From here you have to use the RecurrencePattern.GetOccurrence(startDate)
method to retrieve individual recurrence items. Once you have an individual
occurrence you can just delete it as usual.

You can use the other properties of the recurrence pattern to traverse the
entire series if you want, using the decoded pattern and GetOccurrence() to
reach each item. If you don't get an item for a specified period you then go
to the Exceptions collection of the pattern and check each item there for an
Exception object that has the OriginalDate that matches the date you want.

Every recurring item in the series that has been modified or deleted lives
in the Exceptions collection.

There are examples of working with a recurrence pattern in the code snippets
in the Object Browser Help for each recurrence pattern property.
 
G

Guest

Ken Slovak - said:
Two things first.

If this is Outlook VBA code then don't create an Outlook.Application object,
use the intrinsic Application object exposed in the Outlook VBA.

tnx, how?
Second, when deleting items from a collection never use a For Each loop. By
reducing the size of the collection the internal loop counter is being
messed with, which usually results in deleting only half the items. Use a
count down For loop instead:
Dim i As Long
Dim Count As Long

Count = colCal.Count
For i = Count To 1 Step -1
'etc.
Next

there isn't only recurring appointment, do you conferme whatever your tip?
 
K

Ken Slovak - [MVP - Outlook]

How to use the intrinsic Application object? Just substitute it for any
Outlook.Application object you are creating. Wherever you are now using
objOL just use Application instead. No need to use CreateObject or New on
it.

Whenever you remove any objects from a collection use a down counting loop.




Skipper said:
tnx, how?


there isn't only recurring appointment, do you conferme whatever your tip?
<snip>
 
G

Guest

Ken Slovak - said:
How to use the intrinsic Application object? Just substitute it for any
Outlook.Application object you are creating. Wherever you are now using
objOL just use Application instead. No need to use CreateObject or New on
it.

if i use GetObject fuction ?
 
K

Ken Slovak - [MVP - Outlook]

Just use Application, no CreateObject or New or GetObject.
 
G

Guest

Ken Slovak - said:
Just use Application, no CreateObject or New or GetObject.

sorry, and thanks you for your patient, but i don't understand.
can you make a sample?
TNX
 
G

Guest

Ken Slovak - said:
Just use Application, no CreateObject or New or GetObject.

ok i use:

Set colCal =
Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items


for the single instance of a recurring appointment i try this:
Dim oRecur As Outlook.RecurrencePattern
Dim myException As Outlook.Exception

For i = count To 1 Step -1
Set objAppt = colCal.Item(i)
If objAppt.IsRecurring Then
If objAppt.Categories = "Work" And objAppt.Subject = "Economia"
Then
Set oRecur = objAppt.GetRecurrencePattern()
For j = 1 To oRecur.Occurrences
Set myException = oRecur.Exceptions.Item(j)
<-----------------
MsgBox myException.Subject & " " & myException.Start
Next
End If
End If
Next

but i receive an error on line <------ about "matrix index out of interval"

TNX A LOT
 
K

Ken Slovak - [MVP - Outlook]

Exceptions is a separate collection, it has nothing to do with Occurrences.
Only if every occurrence in the recurring series were modified or deleted
would the 2 counts be equal.

To repeat what I said originally:

From here you have to use the RecurrencePattern.GetOccurrence(startDate)
method to retrieve individual recurrence items. Once you have an individual
occurrence you can just delete it as usual.

You can use the other properties of the recurrence pattern to traverse the
entire series if you want, using the decoded pattern and GetOccurrence() to
reach each item. If you don't get an item for a specified period you then go
to the Exceptions collection of the pattern and check each item there for an
Exception object that has the OriginalDate that matches the date you want.

Every recurring item in the series that has been modified or deleted lives
in the Exceptions collection.

There are examples of working with a recurrence pattern in the code snippets
in the Object Browser Help for each recurrence pattern property.
 
G

Guest

Ken Slovak - said:
To repeat what I said originally:

From here you have to use the RecurrencePattern.GetOccurrence(startDate)
method to retrieve individual recurrence items. Once you have an individual
occurrence you can just delete it as usual.

You can use the other properties of the recurrence pattern to traverse the
entire series if you want, using the decoded pattern and GetOccurrence() to
reach each item. If you don't get an item for a specified period you then go
to the Exceptions collection of the pattern and check each item there for an
Exception object that has the OriginalDate that matches the date you want.

ok, but i don't understand how getoccurrence works. how i can get all the
occurrence one by one, tests if the date it's before today and if true delete
it ?
why i must specify a startdate ? PLZ can you explain with code, i'm not
english native speaker and perhaps i can understand code batter.
TNX
 
G

Guest

WORKS!!!

here how to remove past occurrence:

Dim colCal As Outlook.Items
Set colCal =
Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderCalendar).Items
Dim i, j As Integer

Dim objAppt As Outlook.AppointmentItem

Dim myRecur As Outlook.RecurrencePattern
Dim objRecur As Outlook.AppointmentItem
On Error Resume Next

For i = colCal.count To 1 Step -1
Set objAppt = colCal.Item(i)
If objAppt.IsRecurring Then
Set myRecur = objAppt.GetRecurrencePattern
For j = 0 To myRecur.Occurrences - 1
Set objRecur = myRecur.GetOccurrence(objAppt.Start + j * 7)
If DateDiff("s", objRecur.End, Now()) > 0 Then
MsgBox "Deleted " & objRecur.Subject & " " &
objRecur.Start
objRecur.Delete
End If
Next
End If
Next
 

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