【问题标题】:How do I get the current mail item from Outlook ribbon context menu如何从 Outlook 功能区上下文菜单中获取当前邮件项目
【发布时间】:2011-10-15 11:13:59
【问题描述】:

我正在创建一个 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;
}

有人知道这里发生了什么吗?谢谢!

【问题讨论】:

    标签: c# vsto outlook-addin


    【解决方案1】:

    以下链接可能会为您提供一些见解:

    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;
    }
    

    希望这会有所帮助。

    【讨论】:

    • 这在阅读窗格中不起作用 b/c 上下文是选择的一个实例。但你让我走上了正轨,谢谢!
    【解决方案2】:

    当我不知道什么是动态 ComObject 时,我会使用它。

    添加对 Microsoft.VisualBasic 的引用

    private void whatType(object obj)
    {           
      System.Diagnostics.Debug.WriteLine(Microsoft.VisualBasic.Information.TypeName(obj));
    }
    

    几乎和你一样需要它,我的 IRibbonControl.Context 实际上也是一个选择,尽管它只被选中了一个项目。

    【讨论】:

      【解决方案3】:

      您可以在从所选邮件项的上下文菜单中触发单击事件后检索邮件项-

      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;
                      }
              }
      }
      

      更多详情请访问here

      【讨论】:

        【解决方案4】:

        如果你想在 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;
        

        就我而言,“全局”对我有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-08
          • 1970-01-01
          • 2013-03-04
          • 1970-01-01
          相关资源
          最近更新 更多