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

时间:2019-03-22 14:22:45

标签: c# outlook-addin office-addins

我正在开发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;

}

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

3 个答案:

答案 0 :(得分: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;
}

然后您可以通过以下方式将项目添加到组合框

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))
{
    ...
}

答案 1 :(得分:0)

我假设mailBoxes是您的组合框,然后

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
}

答案 2 :(得分: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);