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

C# 无法将“System.String”类型的对象强制转换为“Microsoft.Office.Interop.Outlook.Store”类型,c#,outlook-addin,office-addins,C#,Outlook Addin,Office Addins,我正在开发Outlook加载项,它将所有可用的共享邮箱填充到组合框中,并使用所选邮箱发送电子邮件 当我从组合框中选择邮件帐户时,我得到一个错误 无法将“System.String”类型的对象强制转换为“Microsoft.Office.Interop.Outlook.Store”类型 下面是代码。 填充组合框 private void MailBoxOptions_Load(object sender, EventArgs e) { Microsoft.Office.Interop.Ou

我正在开发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.");
        }
    }
}
任何帮助都将不胜感激。 谢谢。

我假设邮箱就是你的组合框

mailBoxes.Items.Add(store.DisplayName);
仅将DisplayName字符串添加到组合框中。你现在的问题是把它们放回邮箱。 如果不能有两个同名邮箱,我建议使用字典

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 .where c=>c.ExchangeStoreType==Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olExchangeMailbox 在comboBox1中,您选择的索引已更改

你可以试试:

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);
带有邮箱.Items.Addstore.DisplayName;您正在将存储的显示名称作为字符串添加到组合框中。这就是邮箱的回报。选择编辑。当然,您不能将此字符串强制转换为存储

您可以将存储包装在显示类中

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

嗨,爱达卡,谢谢你的回复。现在它在邮箱中显示错误。SelectedItem参数1:无法从“object”转换为“string”。对不起,我忘记将所选项目解析为字符串。我编辑了我的答案,很高兴能帮上忙。别忘了点击复选标记接受最有用的答案。你调试代码了吗?因为存储类型有太多的用途,而这些用途并不确定是哪种代码造成的。
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))
{
    ...
}