救命!我一直得到“sepcified cast is not valid” [英] Help! I keep getting "sepcified cast is not valid"

查看:87
本文介绍了救命!我一直得到“sepcified cast is not valid”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看我的Outlook(2003)地址簿。代码在项目列表中显示为

并打印所有姓氏,但在点击114

(out或126个联系人)后,我得到一个sepcified cast无效 ;例外

抛出。如果所有的物品集合应该包含?b $ b,那怎么可能无效?我尝试使用一个以Items.GetFirst()开头的while循环,然后

GetNext()但它只获取/打印第一个项目然后循环永久打印

out empty last last姓名。


任何帮助都会非常感激。

谢谢,

jim


我正在使用以下代码:


使用Microsoft.Office.Interop.Outlook; //这是最高的


NameSpace objNamespace = null;

MAPIFolder objFolder = null;

ContactItem lItem;

申请objOutlook;


尝试

{

objOutlook = new ApplicationClass();

objNamespace = objOutlook.GetNamespace(" MAPI");

objFolder =

objNamespace.GetDefaultFolder(OlDefaultFolders.olF olderContacts);

MAPIFolder lLandscaperFolder = objFolder;

//lLandscaperFolder.Items

Debug.WriteLine(objFolder.Items.Count +"找到联系人。);

foreach(lLandscaperFolder.Items中的System.Object _item)

{

lItem =

(Microsoft.Office.Interop.Outlook .ContactItem)_ite m; // lLandscaperFolder.Item

s.Item(i);

Debug.WriteLine(lItem.LastName," Debug" + liDebug。 ToString());

liDebug ++;

}

}

catch(例外e)

{

Syste m.Diagnostics.Trace.WriteLine(e.Message,Exception);

}

解决方案

你好Jim ,


我会试试这个:

在 string s = e.Message;行和看到的类型

_item它可能不是Microsoft.Office.Interop.Outlook.ContactItem


为什么?

我真的不知道,因为我没有使用MAPI,但它可以给你一个关于发生了什么的想法。


foreach(System.Object _item在lLandscaperFolder.Items)

{

尝试{

lItem =

(Microsoft.Office.Interop.Outlook .ContactItem)_ite m; // lLandscaperFolder.Item

s.Item(i);

} catch(例外e)

{

string s = e.Message;

}


}


欢呼,


-

Ignacio Machin,

ignacio.machin at dot.state.fl.us

佛罗里达州交通部


" Jim H" <无**** @ jimsaccount.com>在消息中写道

新闻:Oy ************** @ TK2MSFTNGP09.phx.gbl ...

我想去通过我的Outlook(2003)地址簿。代码通过Items列表打印并打印所有姓氏,但在它打到114
(out或126个联系人)后,我得到一个sepcified cast无效。异常
抛出。当所有的项目集合
应包含时,它怎么能无效?我尝试使用一个以Items.GetFirst()开头的while循环,然后是
GetNext(),但它只获取/打印第一个项目然后永远循环
打印出空的姓氏。

任何帮助都会非常感激。
谢谢,
jim

我正在使用以下代码:

使用Microsoft.Office。 Interop.Outlook; //这是最高的

NameSpace objNamespace = null;
MAPIFolder objFolder = null;
ContactItem lItem;
应用程序objOutlook;

尝试
{
objOutlook = new ApplicationClass();
objNamespace = objOutlook.GetNamespace(" MAPI");
objFolder =
objNamespace.GetDefaultFolder(OlDefaultFolders.olF oldContacts);
MAPIFolder lLandscaperFolder = objFolder;
//lLandscaperFolder.Items
Debug.WriteLine(objFolder.Items.Count +" Contacts finds。);
foreach( lLandscaperFolder.Items中的System.Object _item
{
lItem =

(Microsoft.Office.Interop.Outlook.ContactItem)_ite m; // lLandscaperFolder.Item s.Item (i);
Debug.WriteLine(lItem.LastName," Debug" + liDebug.ToString());
liDebug ++;
}
}
catch (例外e)
{
System.Diagnostics.Trace.WriteLine(e.Message,Exception);
}





那不告诉我什么。它只是说它是'System._ComObject'


这段代码在另一个库中工作,这就是为什么我不明白为什么它不是

在这里工作。


jim


Ignacio Machin(.NET / C#MVP)" < ignacio.machin AT dot.state.fl.us>在消息新闻中写了

:uH ************** @ TK2MSFTNGP10.phx.gbl ...

嗨吉姆,

我会试试这个:
在 string s = e.Message;行和看到_item的类型
它可能不是Microsoft.Office.Interop.Outlook.ContactItem

为什么?
我真的不知道因为我没有使用MAPI但是它可以让你对正在发生的事情有所了解。

foreach(lLandscaperFolder.Items中的System.Object _item)
{
试试{
lItem =

(Microsoft.Office.Interop.Outlook.ContactItem)_ite m; // lLandscaperFolder.Item s.Item(i);
} catch(例外e)
{
string s = e.Message;
}


欢呼,

-
Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局

Jim H <无**** @ jimsaccount.com>在消息中写道
新闻:Oy ************** @ TK2MSFTNGP09.phx.gbl ...

我试图通过我的Outlook( 2003)通讯录。代码通过Items列表打印并打印所有姓氏,但在它点击
114(out或126个联系人)后,我得到一个sepcified cast无效。异常
抛出。当'blockquote class =post_quotes>包含的所有Items集合


时,它怎么能无效?我尝试使用while循环开始使用Items.GetFirst(),
然后GetNext()但它只获取/打印第一个项目然后永远循环


打印

空名。

任何帮助都会非常感激。
谢谢,
jim

我正在使用以下代码:

使用Microsoft.Office.Interop.Outlook; //这是最高的

NameSpace objNamespace = null;
MAPIFolder objFolder = null;
ContactItem lItem;
应用程序objOutlook;

尝试
{
objOutlook = new ApplicationClass();
objNamespace = objOutlook.GetNamespace(" MAPI");
objFolder =
objNamespace.GetDefaultFolder(OlDefaultFolders.olF oldContacts);
MAPIFolder lLandscaperFolder = objFolder;
//lLandscaperFolder.Items
Debug.WriteLine(objFolder.Items.Count +" Contacts finds。);
foreach( lLandscaperFolder.Items中的System.Object _item
{
lItem =



(Microsoft.Office.Interop.Outlook.ContactItem)_ite m; //lLandscaperFolder.Item

s.Item(i);
Debug.WriteLine(lItem.LastName," Debug" + liDebug.ToString( ));
liDebug ++;
}
}
catch(例外e)
{
System.Diagnostics.Trace.WriteLine(e.Message,Exception);
}




嗨吉姆,


这样做,第一次追踪,并在当地人或快速浏览中查看/>
_item的实际类型应该是
Microsoft.Office.Interop.Outlook.ContactItem或从中派生的类型。现在

运行它并看到它在catch区块中遇到断点,并且做同样的事情,我强烈认为你会得到不同的结果。


干杯,

-

Ignacio Machin,
ignacio.machin at dot.state。佛罗里达州交通局

Jim H <无**** @ jimsaccount.com>在消息中写道

news:eL ************** @ TK2MSFTNGP10.phx.gbl ...


那个什么都不告诉我。它只是说它是'System._ComObject'

这段代码在另一个lib中工作,这就是为什么我不明白为什么
在这里不起作用。

Ignacio Machin(.NET / C#MVP) < ignacio.machin AT dot.state.fl.us>
在留言新闻中写道:uH ************** @ TK2MSFTNGP10.phx.gbl ...

嗨吉姆,
在 string s = e.Message;行看到

_item的类型


它可能不是Microsoft.Office.Interop.Outlook.ContactItem

为什么?
我真的不知道因为我没有使用MAPI但它可以给你
一个

关于发生了什么的想法。

foreach(lLandscaperFolder.Items中的System.Object _item)
{
尝试{
lItem =



(Microsoft.Office .Interop.Outlook.ContactItem)_ite m; // lLandscaperFolder.Item

s.Item(i);
} catch(例外e)
{
string s = e.Message;
}


欢呼,

-
ignacio.machin AT dot.state.fl.us
佛罗里达州交通局

Jim H <无**** @ jimsaccount.com>在消息中写道
新闻:Oy ************** @ TK2MSFTNGP09.phx.gbl ...

我试图通过我的Outlook( 2003)通讯录。代码
遍历Items列表并打印所有姓氏,但在它命中后


114(out或126个联系人)我得到一个sepcified cast无效 ;异常
抛出。当'blockquote class =post_quotes>包含的所有Items集合


时,它怎么能无效?我尝试使用while循环开始使用Items.GetFirst(),然后使用GetNext(),但它只获取/打印第一个项目然后永远循环


打印

out out last last名字。

任何帮助都会非常感激。
谢谢,
jim

我正在使用以下代码:
<使用Microsoft.Office.Interop.Outlook; //这是最高的

NameSpace objNamespace = null;
MAPIFolder objFolder = null;
ContactItem lItem;
应用程序objOutlook;

尝试
{
objOutlook = new ApplicationClass();
objNamespace = objOutlook.GetNamespace(" MAPI");
objFolder =
objNamespace.GetDefaultFolder(OlDefaultFolders.olF oldContacts);
MAPIFolder lLandscaperFolder = objFolder;
//lLandscaperFolder.Items
Debug.WriteLine(objFolder.Items.Count +" Contacts finds。);
foreach( lLandscaperFolder.Items中的System.Object _item
{
lItem =



(Microsoft.Office.Interop.Outlook.ContactItem )_ite m; // lLandscaperFolder.Item

s.Item(i);
Debug.WriteLine(lItem。 LastName,Debug" +
liDebug.ToString()); liDebug ++;
}
}
catch(例外e)
{
System.Diagnostics.Trace.WriteLine(e.Message,Exception);
}





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.olF olderContacts);
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)_ite m;//lLandscaperFolder.Item
s.Item(i);
Debug.WriteLine(lItem.LastName, "Debug " + liDebug.ToString());
liDebug++;
}
}
catch(Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message, "Exception");
}

解决方案

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)_ite m;//lLandscaperFolder.Item
s.Item(i);
}catch(Exception e)
{
string s = e.Message;
}

}

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jim H" <no****@jimsaccount.com> wrote in message
news:Oy**************@TK2MSFTNGP09.phx.gbl...

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.olF olderContacts);
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)_ite m;//lLandscaperFolder.Item s.Item(i);
Debug.WriteLine(lItem.LastName, "Debug " + liDebug.ToString());
liDebug++;
}
}
catch(Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message, "Exception");
}




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

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uH**************@TK2MSFTNGP10.phx.gbl...

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)_ite m;//lLandscaperFolder.Item s.Item(i);
}catch(Exception e)
{
string s = e.Message;
}

}

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jim H" <no****@jimsaccount.com> wrote in message
news:Oy**************@TK2MSFTNGP09.phx.gbl...

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.olF olderContacts);
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)_ite m;//lLandscaperFolder.Item

s.Item(i);
Debug.WriteLine(lItem.LastName, "Debug " + liDebug.ToString());
liDebug++;
}
}
catch(Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message, "Exception");
}




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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jim H" <no****@jimsaccount.com> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...


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

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:uH**************@TK2MSFTNGP10.phx.gbl...

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)_ite m;//lLandscaperFolder.Item

s.Item(i);
}catch(Exception e)
{
string s = e.Message;
}

}

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jim H" <no****@jimsaccount.com> wrote in message
news:Oy**************@TK2MSFTNGP09.phx.gbl...

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.olF olderContacts);
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)_ite m;//lLandscaperFolder.Item

s.Item(i);
Debug.WriteLine(lItem.LastName, "Debug " + liDebug.ToString()); liDebug++;
}
}
catch(Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message, "Exception");
}





这篇关于救命!我一直得到“sepcified cast is not valid”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆