【问题标题】:Accessing Contacts From .OST File Office 2010/2013从 .OST File Office 2010/2013 访问联系人
【发布时间】:2015-01-07 05:51:33
【问题描述】:

让我先说我不使用办公室 COM 对象,并且通常会像瘟疫一样尽量避免它。我也知道这个问题在访问 .pst 文件方面已经被打死了,但是经过一个小时的谷歌搜索后,我还没有找到很多关于 .ost 的信息。

我正在尝试更新现有应用程序中的方法,该应用程序正在访问 .pst 文件并检索要在自动填充实现中使用的联系人姓名和电子邮件列表。我们最近从直接 IMAP 更改为主机电子邮件服务器到 Microsoft Exchange,后者从 .pst 更改为 .ost 文件

这是方法:

    var arrName = new List<string>();
    var arrEmail = new List<string>();
    try
    {
        var outlookApplication = new ApplicationClass();
        NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
        MAPIFolder contacts = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);

         for (int i = 1; i < contacts.Items.Count + 1; i++)
         {
             var contact = (ContactItem) contacts.Items[i];
             arrName.Add(contact.FullName);
             arrEmail.Add(contact.Email1Address);
         }
            Global.ConName = arrName.ToArray();
          Global.ConEmail = arrEmail.ToArray();
        }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.ToString());
        //Error Logging
    }

调用时抛出以下错误:

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to 
interface type 'Microsoft.Office.Interop.Outlook.ContactItem'. 
This operation failed because the QueryInterface call on the COM 
component for the interface with IID '{00063021-0000-0000-C000-000000000046}' 
failed due to the following error: 
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

at OPUSfin.LoginMain.GetEmailContactsFromOutlook() in PATH:line 81

谁能指出我如何修改它以与 Exchange 实现一起使用的正确方向?

谢谢

参考:Accessing Outlook ost file Question

【问题讨论】:

    标签: c# outlook office-interop


    【解决方案1】:

    除了代码期望的 ContactItem 之外,您还可以拥有 DistListItem 对象。

    使用“as”运算符来检查您是否真的有一个 ContactItem 对象。 您可能还想在进入循环之前缓存 Items 集合:

    Items items = contacts.Items;
    for (int i = 1; i <= items.Count; i++)
    {
        ContactItem contact = items[i] as ContactItem;
        if (contact !=null)
        {
           ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-17
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多