Help! I keep getting "sepcified cast is not valid"

J

Jim H

I am trying to go through my Outlook (2003) address book. The code goes
through the Items list and prints all the last names but after it hits 114
(out or 126 contacts) I get a "sepcified cast is not valid" exception
thrown. How can it not be valid when that's all the Items collection should
contain? I tried using a while loop starting with Items.GetFirst(), then
GetNext() but it only gets/prints the first item then loops forever printing
out empty last names.

Any help would be GREATLY appreciated.
Thanks,
jim

I'm using the following code:

using Microsoft.Office.Interop.Outlook; //this is up top

NameSpace objNamespace = null;
MAPIFolder objFolder = null;
ContactItem lItem;
Application objOutlook;

try
{
objOutlook = new ApplicationClass();
objNamespace = objOutlook.GetNamespace("MAPI");
objFolder =
objNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
MAPIFolder lLandscaperFolder = objFolder;
//lLandscaperFolder.Items
Debug.WriteLine(objFolder.Items.Count + " Contacts found.");
foreach(System.Object _item in lLandscaperFolder.Items)
{
lItem =
(Microsoft.Office.Interop.Outlook.ContactItem)_item;//lLandscaperFolder.Item
s.Item(i);
Debug.WriteLine(lItem.LastName, "Debug " + liDebug.ToString());
liDebug++;
}
}
catch(Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message, "Exception");
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Jim,

I would try this:
put a breakpoint in the " string s = e.Message;" line and see the type of
_item it may not be Microsoft.Office.Interop.Outlook.ContactItem

Why?
I really have no idea as I havent worked with MAPI but it can gove you an
idea of what is going on.

foreach(System.Object _item in lLandscaperFolder.Items)
{
try{
lItem =
(Microsoft.Office.Interop.Outlook.ContactItem)_item;//lLandscaperFolder.Item
s.Item(i);
}catch(Exception e)
{
string s = e.Message;
}

}

cheers,
 
J

Jim H

That doesn't tell me anything. It just says it's a "System._ComObject"

This code works in another lib that's why I don't understand why it doesn't
work here.

jim
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Jim,

Do this, trace it the first time, and see in the locals or in the quickview
the real type of _item it should be o
Microsoft.Office.Interop.Outlook.ContactItem or a type derived from it. now
run it and see it hit the breakpoint in the catch block, and do the same, I
strongly think that you will get different results.

Cheers,
 
J

Jim H

That is how I do it. It always shows up as a "System._ComObject". Even the
ones that successfully cast show up in the locals window as a
"System._ComObject". I drill down into those and still never find anything
that says ContactItem.

Outlook.ContactItem is an interface so it may never show up as a variable
type.

jim
 
E

Erik Frey

Examine the MessageClass member (string) of _item to figure out what
kind of item it is. Then, cast appropriately.

Try this in your Watch window:

_item.MessageClass

Erik
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Thanks for posting in this group.
Based on my understanding, you got the exception "sepcified cast is not
valid" after already getting 114 contacts from address book.
Does your exception throw out at this line: lItem
=(Microsoft.Office.Interop.Outlook.ContactItem)_item; ?
I think you can try to use Outlook._ContactItem interface or
Outlook.ContactItemClass class to replace Outlook.ContactItem interface

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jim H

That is not a member of the _item object. _item is a System._ComObject type
according to the debugger.

jim
 
J

Jim H

Yes it does.

I tried using ContactItemClass and _ContactItem instead but no changes. It
still throws the exception on that line.

jim
 
P

Peter Huang

Hi Jim,

Here is my test code which runs well on my machine, you may have a try and
let me know the result.

using System;
using Outlook=Microsoft.Office.Interop.Outlook;
namespace TestOL
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Outlook.Application olApp= new Outlook.ApplicationClass();
Outlook.MAPIFolder mfd;

mfd=olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olF
olderContacts);
foreach(Outlook.ContactItem cim in mfd.Items )
{
Console.WriteLine(cim.LastName);
}
}
}
}

Or you may try to see if the error is specified to the 114 th item?
You may try to get the item by using the index.
for(int i=0;i<113;i++)
cim =(Outlook.ContactItem)mfd.Items;
for(int i=115;i<mfd.Items.Count;i++)
cim =(Outlook.ContactItem)mfd.Items;


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bored Stiff

I think you'll find that some items e.g. distribution lists in a
Contacts folder are not ContactItems. One solution is to ignore anything
that isn't a ContactItem, e.g.:

lItem = _item as Microsoft.Office.Interop.Outlook.ContactItem;
if (lItem != null)
{
Debug.WriteLine(lItem.LastName, "Debug " + liDebug.ToString());
liDebug++;
}
 
P

Peter Huang

Hi Jim,

You may also try to get the messageclass of the object to judge if this is
an contact item.
using System;
using Outlook=Microsoft.Office.Interop.Outlook;
namespace TestOL
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Outlook.Application olApp= new Outlook.ApplicationClass();
Outlook.MAPIFolder mfd;

mfd=olApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olF
olderContacts);
foreach(object cim in mfd.Items )
{
object
o=cim.GetType().InvokeMember("MessageClass",System.Reflection.BindingFlags.G
etProperty,null,cim,null);
if (String.Equals(o.ToString(),"IPM.Contact"))
{
Outlook.ContactItem con_item = (Outlook.ContactItem)cim;
Console.WriteLine(con_item.LastName);
}
}
}
}
}


If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Top