More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 24-Jan-2018 06:20

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 2 Next >> ]  
 Outlook and .NET Printer Friendly Version
Writing code with C# and VB.NET to create Outlook add-ins and other projects
Specified cast is not valid
Hi, I have a question if its OK. Why did i always get this error message: "Specified cast is not valid."?

I'm trying to get appointment from shared calendar programmatically and upon running my program, i always get this error. But when I open the shared calendar on my Outlook, I can view appointments on that particular calendar I access.
Please I need help.

I'm using PL and Outlook 2003.


  11-Jul-2005  21:20
  12-Jul-2005  07:12   
What statement raises that error?
  13-Jul-2005  01:49   
This one:
            For Each objAppt In colMyAppts
                strOut = strOut & (FormatDateTime(objAppt.Start, DateFormat.ShortTime) & " - " & FormatDateTime(objAppt.End, DateFormat.ShortTime) & _
                    vbTab & objAppt.Subject & vbCrLf & vbTab & vbTab & _
                    "Notes: " & objAppt.Body & vbCrLf & _
                    vbTab & vbTab & "Date Created: " & CStr(objAppt.CreationTime) & vbCrLf & vbCrLf)
  13-Jul-2005  10:18   
You should not assume that any Outlook folder has homogeneous contents. Even a Calendar folder can have another type of item in it. One approach, therefore, is to check the item type before using a specific cast:

Dim itm as Object
For Each itm in colMyAppts
        If TypeOf (itm) Is Outlook.AppointmentItem Then
            Dim appt As Outlook.AppointmentItem = CType(itm, Outlook.AppointmentItem)
            ' your code to work with appt and its AppointmentItem properties follows
       End IF
  13-Jul-2005  19:23   
Ah I see. Okay I'll try this out first. Thank you very much. I really appreciate your help.
  13-Jul-2005  19:39   
What should be the content of Object itm in your declaration? I could not get any return value from the statement. There is no output on my program if I use this lines.
  13-Jul-2005  19:53   
And I still get the same error: Secified cast is not valid.
  14-Jul-2005  07:45   
itm is an object variable to point to an item in the calendar folder. The declaration itself will have no return value.

If colMyAppts is a MAPIFolder.Items collection, as it appears to be in your original code above, then iterating it with a For Each ... Next loop should return an item from that folder. 99% of the time it will be an Outlook.AppointmentItem, but there's always the possibility that another type of item will be present.

Maybe you should show the latest version of your code snippet?
  14-Jul-2005  20:42   
I made changes in my codes and the new lines where added:

colMyAppts = colCal.Restrict(strFind)
Dim itm as Object
      For Each itm In colMyAppts
If TypeOf (itm) Is Outlook.AppointmentItem Then
Dim appt As Outlook.AppointmentItem = CType(itm, Outlook.AppointmentItem)
strOut = CStr(strOut & (FormatDateTime(appt.Start, DateFormat.ShortTime) & " - " & FormatDateTime(appt.End, DateFormat.ShortTime) & _
                    vbTab & CStr(appt.Subject) & vbCrLf & vbTab & vbTab & _
                    "Notes: " & CStr(appt.Body) & vbCrLf & _
                    vbTab & vbTab & "Date Created: " & CStr(appt.CreationTime) & vbCrLf & vbCrLf))
            Return strOut

  14-Jul-2005  20:43   
sorry where -> were =)
  15-Jul-2005  06:57   
It's the strOut = statement that produces the error? Since you have a lot of expressions in that statement, maybe you should break it into pieces and see if there's a problem with one of those expressions. You could, for example, use the StringBuilder class to build and format the string. You could also use expressions like appt.CreationTime.ToString instead of CStr().
    Page [ 1 2 Next >> ]