Outlook Appointment UserProperties runtime error "InvalidCastException"

Orchard used Ask the Experts™
Hi all,

I have an annoying problem, in that some code that has been working really well has stopped working in the last few weeks, and I can't seem to fix it. It still compiles OK, but fails at runtime with an "InvalidCastException" error.

It is a desktop application, and essentially we have a database that holds details of the jobs we do, and also the work that we do for each job. Work is defined as a period of time where we do something towards completing a job. The code reads through Work entries in the database and also all of the user's Outlook appointments looking for matches. When it finds a match, it updates the Outlook appointment. If it does not find a match it creates a new appointment in Outlook.

The match is found by using a UserProperties field called "WID" which holds the Work ID number from the SQL database. What I am finding now is that existing Outlook appointments can be updated just fine, but we get the "InvalidCastExcpetion" error when the code tries to create the UserProperty for a new appointment. As I said, it compiles OK, but fails at runtime.

The code is below - the runtime error occurs at the line 23, and it has been running absolutely fine with Office 2013 and 2010. SRow is declared elsewhere as a DataRow, and holds data from the Work table.

            If Not FoundIt Then
                'we need to add it
                oNewAppt = oApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
                oNewAppt.AllDayEvent = False
                oNewAppt.Subject = SRow.Item("FirstName") & " - " & SRow.Item("wTitle")
                oNewAppt.Body = IIf(IsDBNull(SRow.Item("wNotes")), "", SRow.Item("wNotes"))
                oNewAppt.Location = SRow.Item("DisplayName")
                oNewAppt.Start = SRow.Item("DepartFromOrchardTime")
                oNewAppt.End = SRow.Item("ArriveBackAtOrchardTime")
                If CDate(SRow.Item("DepartFromOrchardTime")) < Today Then
                    'Don't set reminders for historic events
                    oNewAppt.ReminderSet = False
                    'if this is my work, give me a reminder, else don't
                    If SRow.Item("StaffID") = UID Then
                        oNewAppt.ReminderSet = True
                        oNewAppt.ReminderSet = False
                    End If
                End If
                oNewAppt.ReminderMinutesBeforeStart = 15
                oNewAppt.BusyStatus = Outlook.OlBusyStatus.olBusy
                oNewAppt.UserProperties.Add("WID", Outlook.OlUserPropertyType.olInteger, False, GetType(Outlook.OlFormatInteger))

                oNewAppt.UserProperties("WID").Value = SRow.Item("WID")
                Select Case SRow.Item("WorkStatus")
                    Case 1, 2
                        oNewAppt.Categories = SRow.Item("WorkStatus") & SRow.Item("WorkType")
                    Case 6
                        oNewAppt.Categories = SRow.Item("WorkStatus")
                    Case Else
                        If CInt(SRow.Item("WorkType")) >= 10 Then
                            oNewAppt.Categories = SRow.Item("WorkType")
                            oNewAppt.Categories = SRow.Item("WorkStatus")
                        End If
                End Select
            End If

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You code snippet does not show how you declare some of your variables.  Do you have a reference in your application to the Outlook object library?  Also, without your datasource, I cannot completely test your code.  Maybe another expert can see something I am missing right off.  With a reference, you really do not need a variable of oApp.  Therefore I suggest the start of your code be something like this:

    Dim oNewAppt As Outlook.AppointmentItem
    Set oNewAppt = Outlook.CreateItem(Outlook.olAppointmentItem)

Open in new window

Then when setting the user property, change how you set the display format and set the value in the Add statement like this:
                oNewAppt.UserProperties.Add("WID", Outlook.OlUserPropertyType.olInteger, False, Outlook.OlUserPropertyType.olInteger) = SRow.Item("WID")

Open in new window

Give those changes a try and see what happens.  If they do not help, you might want to share how you declare your variables.


Many thanks for your answer,  ltlbearand3.

Yes, I declare the variable further up the page - I omitted the code so that there isn't so much to wade through.

Up at the top of the page I have:
Imports System.Data
Imports System.Data.SqlClient
Imports Calendar
Imports Microsoft.Office.Interop
Imports System.Text

Open in new window

and then in the subroutine that I showed before I have the following:

        Dim oApp As Outlook.Application = New Outlook.Application
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
        Dim oFolder As Outlook.Folder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
        Dim oAppt, oNewAppt As Outlook.AppointmentItem
        Dim OutlookSyncItems As Outlook._Items

Open in new window

not all of which we are using here.

I don't think there is an error in the code - it has been working fine for 3 years or so. Your declaration of the UserProperty is the same as mine, I think, so I don't think there is anything new there.

Could there have been a change in the .Net framework that may affect the way that Outlook uses UserProperties?

Thanks for taking the time for me on this one.
Ok.  I had not realized this was coded in .net.  I don't code often in .net for outlook so I am not sure if an update perhaps changed the way this code would function.  That is a possibility.  I have looked at this again from the .net point of view.  What target .net framework is your application coded to use?  What OS are you using?  What .net framework is installed on your machine?

I ran a test using .net 3.5 as the target framework and also received the invalid cast exception with a mock up of your code.  If I change the line with problems to:
oNewAppt.UserProperties.Add("WID", Outlook.OlUserPropertyType.olInteger, False, Outlook.OlUserPropertyType.olInteger)

Open in new window

the code runs fine.  I suggest using that change and seeing if the code runs now.


Great! Many thanks  ltlbearand3, you have nailed it. It's actually a better statement in the code now, and it makes more sense using the same type of integer declaration in the two parts of the statement. Hopefully Microsoft will update MSDN, now, because it still has examples similar to my original statement.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial