How to create recurring appointment exceptions? VSTO 2k8

M

mikeo

I am writing an Outlook 2007 Add-in using VSTO 2008 (VB .NET). I want to
allow the user to add an appointment that is associated with an existing
recurring appointment. The catch is that the given date may not fit the
recurrence pattern. For example, if the user has an Inspector window open
with an occurence of an "every Thursday" appointment, he may choose to add a
Monday. I know the object model supports it because I can do it manually by
selecting a recurrance and changing the start date.

I have been able to programmatically add a new "non-exception" meeting by
calling AppointmentItem.GetRecurrencePattern and incrementing
recurPattern.Occurrences. I can also refer to the new appointment via
recurPattern.GetOccurence(date). However, I haven't found a way to update
the appointment start date and create an exception. Here is some of the code
I've been trying:

' Add an appointment for this date
Public Sub AddDate(ByVal dt As Date)
Dim inspector As Outlook._Inspector = Nothing
Dim apptItem As Outlook.AppointmentItem = Nothing
Dim recurPattern As Outlook.RecurrencePattern = Nothing
Dim newDate As Date
Dim newApptItem As Outlook.AppointmentItem = Nothing
Dim origEndDate As Date

Try
inspector = OutlookApp.ActiveInspector
Catch
End Try

If inspector IsNot Nothing Then
apptItem = inspector.CurrentItem
If apptItem.IsRecurring Then

Try
' get the recurrence pattern
recurPattern = apptItem.GetRecurrencePattern

' get a new meeting by incrementing the occurrences
recurPattern.Occurrences += 1

' get the newly-created appointment item
newDate = New Date(recurPattern.PatternEndDate.Year, _
recurPattern.PatternEndDate.Month, _
recurPattern.PatternEndDate.Day, _
recurPattern.StartTime.Hour, _
recurPattern.StartTime.Minute, _
recurPattern.StartTime.Second)
newApptItem = recurPattern.GetOccurrence(newDate)

' set the start date of the new meeting to the desired
date
newDate = New Date(dt.Year, dt.Month, dt.Day, _
recurPattern.StartTime.Hour, _
recurPattern.StartTime.Minute, _
recurPattern.StartTime.Second)

' this update gets lost if newDate is out of the
recurrence pattern ....
newApptItem.Start = newDate

Catch
End Try
Else
' need to make it recurring ...
End If
End If

If inspector IsNot Nothing Then Marshal.ReleaseComObject(inspector)
If apptItem IsNot Nothing Then Marshal.ReleaseComObject(apptItem)
If recurPattern IsNot Nothing Then
Marshal.ReleaseComObject(recurPattern)
If newApptItem IsNot Nothing Then
Marshal.ReleaseComObject(newApptItem)
End Sub


Is there a better approach to add an "exception" appointment to an existing
recurrence pattern? Or, to put it another way, is there a way in VSTO to set
the appointment conversationIndex to associate it with other appointments?
Any suggestions are greatly appreciated.

Thanks,
mikeo
 
K

Ken Slovak - [MVP - Outlook]

Changing the date of any existing instance of a recurring series will create
an exception, are you saving the item after applying the change to the start
date?
 
M

mikeo

Hi Ken,

Thanks for your quick reply ...

You raise a good point - when I try to save the item (via
AppointmentItem.Save) I get the following exception (AccessViolationException
is caught by my "catch" statement) and the inspector window closes:

"Attempted to read or write protected memory. This is often an indication
that other memory is corrupt."

I find that the "non-exception" item that created, but the start date wasn't
updated. I assume that I'm not allowed in VSTO to explicitly save this item
that Outlook created via COM when I add a recurrence ... or could it be
related to releasing/not releasing COM objects appropriately?

Thanks a lot for your help,
Mike
 
K

Ken Slovak - [MVP - Outlook]

I don't see VSTO preventing any saves of items. It's mostly a shim loader
and wrapper for unhandled exceptions and for the IDTExtensibility
interfaces.

I think the problem is your algorithm. If you extend the number of
occurrences the new item goes at the end of the series. You are trying to
reschedule the new appointment to some other date, triggering the Outlook
error: "cannot reschedule an occurrence if it skips over a later occurrence
of the same appointment". That's the problem.

What you'd have to do is to change the occurrence just after the new desired
date to that date, change the following appointment to the date of the
occurrence you just changed, repeat for every occurence in the series to
avoid that error. It would be hell to manage and code, if it would even work
at all.

And if it did work you'd end up with the entire series being exceptions.

As I said, this has nothing to do with VSTO or Outlook version. It's a
limitation of the implementation of RecurrencePattern and its binary blob.
 
M

mikeo

Hi Ken,

Thanks again for your insightful feedback.

I'm not convinced that this is the specific issue. Since the addition of
exception items is fundamental to the requirements for my add-in, I'm not
ready to throw in the towel yet ...

I receive the protected memory exception even if the item I'm trying to
create/save remains as the last item in the pattern. For example, the last
recurrence of a weekly recurring meeting is on Friday, May 9th. When my code
increments recurPattern.Occurrences by 1, Outlook creates a new occurence on
Friday, May 16th. When I programmatically try to update the Start date of
this new item to Thu May 15, I get the error. Note that Outlook lets me do
this manually (i.e. increment Occurrences by 1, save, open the new last
recurrence, update date, save.

Any other theories as to what is going wrong here? Debugging suggestions?

Thanks,
mikeo
 
K

Ken Slovak - [MVP - Outlook]

No other suggestions, my guess is that Outlook doesn't like your
incrementing Occurrences in code. But that's just a guess.
 

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