Solved

Outlook Appointment UserProperties runtime error "InvalidCastException"

Posted on 2014-03-05
6
477 Views
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
                Else
                    'if this is my work, give me a reminder, else don't
                    If SRow.Item("StaffID") = UID Then
                        oNewAppt.ReminderSet = True
                    Else
                        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")
                        Else
                            oNewAppt.Categories = SRow.Item("WorkStatus")
                        End If
                End Select
                oNewAppt.Save()
            End If

Open in new window

0
Comment
Question by:Orchard
  • 2
  • 2
6 Comments
 
LVL 20

Expert Comment

by:ltlbearand3
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.
0
 

Author Comment

by:Orchard
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.
0
 
LVL 20

Accepted Solution

by:
ltlbearand3 earned 500 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.
0
 

Author Closing Comment

by:Orchard
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.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
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 …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

856 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