我正在开发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;
}
任何帮助,将不胜感激。谢谢。
我假设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
}
使用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))
{
...
}
如果你能在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);