无法将类型 'System.String' 的对象转换为类型 'Microsoft.Office.Interop.Outlook.Store

Unable to cast object of type 'System.String' to type 'Microsoft.Office.Interop.Outlook.Store

我正在开发 Outlook 加载项,它将所有可用的共享邮箱填充到组合框中,并使用 selected 邮箱发送电子邮件。

当我从组合框中 select 邮件帐户时,出现错误

Unable to cast object of type 'System.String' to type 'Microsoft.Office.Interop.Outlook.Store'

代码如下。 填充组合框。

private void MailBoxOptions_Load(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Outlook.Application application =
        new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook.NameSpace ns = application.GetNamespace("MAPI");
    Stores stores = ns.Stores;
    foreach (var store in Globals.ThisAddIn.Application.Session.Stores
        .Cast<Microsoft.Office.Interop.Outlook.Store>()
        .Where(c => c.ExchangeStoreType == 
                      Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olExchangeMailbox))
    {
        if (store != null)
        {
            mailBoxes.Items.Add(store.DisplayName);
        }
        else
        {
            MessageBox.Show("You don't have access to any shared mail-inbox.");
        }
    }
}

组合框代码

public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var selectedStore = (Store)mailBoxes.SelectedItem;

}

如有任何帮助,我们将不胜感激。 谢谢。

我假设 mailBoxes 是您的 ComboBox 那么

mailBoxes.Items.Add(store.DisplayName);

仅将 DisplayName 字符串添加到 ComboBox。你现在的问题是把那些放回邮箱。
如果不能有两个同名的邮箱,我建议使用 Dictionary

private Dictionary<string, Store> storeDictionary = new Dictionary<string, Store>();

private void MailBoxOptions_Load(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook.NameSpace ns = application.GetNamespace("MAPI");
    Stores stores = ns.Stores;
    foreach (var store in Globals.ThisAddIn.Application.Session.Stores.Cast<Microsoft.Office.Interop.Outlook.Store>().Where(c => c.ExchangeStoreType == Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olExchangeMailbox))
    {
        if (store != null)
        {
            mailBoxes.Items.Add(store.DisplayName);
            storeDictionary.Add(store.DisplayName, store); // Add the items to the dictionary
        }
        else
        {
            MessageBox.Show("You don't have access to any shared mail-inbox.");
        }
    }
}

然后从字典Store中获取

public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(!mailBoxes.SelectedItem is string selectedString))
        return;

    bool successful = storeDictionary.TryGetValue(selectedString, out Store selectedStore);
    if(!successful)
    {
        return;
    }
    // Access selectedStore here
}

如果您可以访问 Globals.ThisAddIn.Application.Session.Stores.Cast<Microsoft.Office.Interop.Outlook.Store>() .Where(c => c.ExchangeStoreType == Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olExchangeMailbox)comboBox1_SelectedIndexChanged

你可以试试:

var selectedStore = Globals.ThisAddIn.Application.Session.Stores.Cast<Microsoft.Office.Interop.Outlook.Store>()
   .Where(c => c.ExchangeStoreType == Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olExchangeMailbox)
   .SingleOrDefault(x => x.DisplayName == mailBoxes.SelectedItem);

使用 mailBoxes.Items.Add(store.DisplayName);,您将商店的显示名称添加为 string 到 ComboBox。这正是您在 return 和 mailBoxes.SelectedItem 中得到的结果。当然,您不能将此字符串转换为 Store.

您可以将商店包裹在展示中 class

public class StoreDisplay
{
    public StoreDisplay(Store store)
    {
        this.Store = store;
    }

    public Store Store { get; }

    public override string ToString() ==> Store.DisplayName;
}

然后您可以使用

将项目添加到 ComboBox
mailBoxes.Items.Add(new StoreDisplay(store));

因为 ToString 已被覆盖,组合框将显示 DisplayName 或每个商店项目。

最后,您可以使用

检索商店
var selectedStore = ((StoreDisplay)mailBoxes.SelectedItem)?.Store;
if (selectedStore != null) {
    ...
}

您也可以尝试将Store对象直接添加到ComboBox中;不过不知道能不能正常显示。


旁注:如果您有冲突的类型名称,或者如果您只是想要更短的命名空间引用,您可以使用命名空间别名

using MsOl = Microsoft.Office.Interop.Outlook;
using AppSession = Globals.ThisAddIn.Application.Session;

并像这样使用它

var application = new MsOl.Application();
MsOl.NameSpace ns = application.GetNamespace("MAPI");
Stores stores = ns.Stores;
foreach (var store in AppSession.Stores
    .Cast<MsOl.Store>()
    .Where(c => c.ExchangeStoreType == MsOl.OlExchangeStoreType.olExchangeMailbox))
{
    ...
}