Orchard
asked on
Outlook Appointment UserProperties runtime error "InvalidCastException"
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.
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
ASKER
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:
and then in the subroutine that I showed before I have the following:
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.
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
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
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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:
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.