Specified cast is not valid

  • Thread starter Janni
  • Start date Views 2,750
Status
Not open for further replies.
J

Janni

I'm developing a VSTO (second edition) addin for Outlook. One of my

customers gets the following error:

"System.InvalidCastException: Specified cast is not valid.

at

System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal

(Int32 errorCode, IntPtr errorInfo)

at

System.Runtime.InteropServices.CustomMarshalers.EnumerableViewOfDispatch.GetEnumerator

()

at System.Collections.IEnumerable.GetEnumerator() "

when looping through the items in the calendar folder.

Dim oFolder As MsOutlook.MAPIFolder = oSession.GetDefaultFolder

(Outlook.OlDefaultFolders.olFolderCalendar)

Dim oItems As MsOutlook.Items = m_oFolder.Items

For Each oItem As Object In oItems

> ...

Next

Anyone who has any clue about the cause of this error?
 
K

Ken Slovak - [MVP - Outlook]

What line is the exception thrown on? Is it in the For Each line?

Does the exception get thrown if the code is changed to use a normal For

loop with a loop counter?

Dim count as Integer = oItems.Count

Dim i As Integer

For i = 1 To count

' code here

Next

"Janni" <janni.kajbrink@hotmail.com> wrote in message

news:11495981-e5ec-4efa-b616-c698bef62550@g37g2000yqn.googlegroups.com...
> I'm developing a VSTO (second edition) addin for Outlook. One of my
> customers gets the following error:

> "System.InvalidCastException: Specified cast is not valid.
> at
> System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal
> (Int32 errorCode, IntPtr errorInfo)
> at
> System.Runtime.InteropServices.CustomMarshalers.EnumerableViewOfDispatch.GetEnumerator
> ()
> at System.Collections.IEnumerable.GetEnumerator() "

> when looping through the items in the calendar folder.

> Dim oFolder As MsOutlook.MAPIFolder = oSession.GetDefaultFolder
> (Outlook.OlDefaultFolders.olFolderCalendar)
> Dim oItems As MsOutlook.Items = m_oFolder.Items

> For Each oItem As Object In oItems
> ...
> Next

> Anyone who has any clue about the cause of this error?
>
 
Status
Not open for further replies.
Top