[英]How do I get the current mail item from Outlook ribbon context menu
我正在創建一個 Outlook 2010 加載項,並在我的功能區中為 idMso="contextMenuMailItem" 添加了一個上下文菜單。 單擊時,我想刪除一個類別,但在單擊事件處理程序中,當我將 ctl.Context 轉換為 MailItem 時,它始終為空。
public bool btnRemoveCategory_IsVisible(Office.IRibbonControl ctl)
{
MailItem item = ctl.Context as MailItem; //Always null
if (item != null)
return (item != null && HasMyCategory(item));
else
return false;
}
有誰知道這里發生了什么? 謝謝!
以下鏈接可能會為您提供一些見解:
http://msdn.microsoft.com/en-us/library/ff863278.aspx
控件的“上下文”為您提供了您正在自定義的相應 Outlook 對象(例如 Inspector 對象)。 從那里您需要引用上下文對象的 CurrentItem 屬性來獲取 MailItem。
例如,
public bool btnRemoveCategory_IsVisible(Office.IRibbonControl ctl)
{
var item = ctl.Context as Inspector;
var mailItem = item.CurrentItem as MailItem;
if (item != null)
return (item != null && HasMyCategory(item));
else
return false;
}
希望這會有所幫助。
您可以在從所選郵件項目的上下文菜單中觸發單擊事件后檢索郵件項目 -
public bool btnRemoveCategory_IsVisible(Office.IRibbonControl ctl)
{
Explorer explorer = Globals.ThisAddIn.app.ActiveExplorer();
if (explorer != null && explorer.Selection != null && explorer.Selection.Count > 0)
{
object item = explorer.Selection[1];
if (item is MailItem)
{
MailItem mailItem = item as MailItem;
}
}
}
欲了解更多詳情,請訪問此處。
當我無法弄清楚動態 ComObject 是什么時,我會使用它。
添加對 Microsoft.VisualBasic 的引用
private void whatType(object obj)
{
System.Diagnostics.Debug.WriteLine(Microsoft.VisualBasic.Information.TypeName(obj));
}
只是因為幾乎和你一樣的事情需要它,我的 IRibbonControl.Context 實際上也是一個選擇,盡管它只選擇了一個項目。
如果您想在 Ribbon.cs 中引用 TheAddin,也許您還可以考慮使用“Globals”。
例如,假設您在解決方案資源管理器中有以下文件:
Outlook
- ThisAddin.cs
Ribbon1.cs
在“ThisAddin.cs”中聲明一個公共 MailItem 並將郵件項目分配給它:
public MailItem myMail = null;
...
myMail=....
然后在 Ribbon1.cs 中使用“Globals”訪問它
MailItem item=Globals.ThisAddin.myMail;
就我而言,“全局變量”對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.