[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Outlook Appointment UserProperties runtime error "InvalidCastException"

Posted on 2014-03-05
Medium Priority
Last Modified: 2014-03-12
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

Question by:Orchard
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 20

Expert Comment

ID: 39919321
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.

Author Comment

ID: 39920380
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.
LVL 20

Accepted Solution

ltlbearand3 earned 2000 total points
ID: 39922227
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.

Author Closing Comment

ID: 39922815
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question