无法将“System.String”类型的对象强制转换为“Microsoft.Office.Interop.Outlook.Store”

问题描述 投票:0回答:3

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

当我从组合框中选择邮件帐户时,我收到错误

无法将类型为“System.String”的对象强制转换为“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;

}

任何帮助,将不胜感激。谢谢。

c# outlook-addin office-addins
3个回答
0
投票

我假设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
}

1
投票

使用mailBoxes.Items.Add(store.DisplayName);,您将商店的显示名称添加为string到ComboBox。这与你在mailBoxes.SelectedItem的回报是显而易见的。当然你不能把这个字符串强制转换为Store

您可以将商店包装在显示类中

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已被覆盖,ComboBox将显示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))
{
    ...
}

0
投票

如果你能在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);
© www.soinside.com 2019 - 2024. All rights reserved.