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");
}
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");
}