【问题标题】:How to get the titles of all opened inspectors in outlook?如何在outlook中获取所有打开的inspector的title?
【发布时间】:2021-10-19 09:03:29
【问题描述】:

C#、VSTO、Outlook 2016。

我想获取 Outlook 中所有打开的 Inspector 的标题列表。

第一次调用 Outlook.Application.Inspectors[1].Caption 返回“Message”作为检查器标题。 但是检查员的窗口标题显示了电子邮件的主题。

但是当我打开更多检查器时,标题似乎会改变。

所以集合中总是有一个检查员没有 电子邮件主题和 Inspector.Caption 与对应的不匹配 窗口标题。

那么我怎样才能得到正确的窗口标题列表呢?

对应代码:

  private void ThisAddIn_Startup(object sender, System.EventArgs e)
  {
    m_Application = this.Application as Outlook.Application;
    m_Inspectors  = m_Application.Inspectors;

    m_Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(m_Inspectors_NewInspector);

    void m_Inspectors_NewInspector(Outlook.Inspector Inspector) {
      if (Application.Inspectors.Count > 0) {
        Debug.WriteLine("\n=== [Test] ================================");
        Debug.WriteLine($" Inspectors.Count: {Application.Inspectors.Count}");
        Debug.WriteLine(" Application.Inspectors: ");
        foreach (Outlook.Inspector CurrentInspector in Application.Inspectors) {
          Debug.WriteLine($"   {CurrentInspector.Caption}");
          Debug.WriteLine($"   --> {CurrentInspector.CurrentItem.Subject}");
        }
        Debug.WriteLine("===========================================\n");
      }
    }
  }

【问题讨论】:

    标签: c# outlook outlook-addin


    【解决方案1】:

    感谢 Dmitry Streblechenko,这可以完成工作:

    供参考:

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          Debug.WriteLine("[Test]: startet");
    
          // Variables
          m_Application = this.Application as Outlook.Application;
          m_Inspectors = m_Application.Inspectors;
    
          // Events
          m_Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(m_Inspectors_NewInspector);
        }
    
        void m_Inspectors_NewInspector(Outlook.Inspector Inspector) {
          Outlook.Inspector m_Inspector = Inspector;
          ((Outlook.InspectorEvents_Event) m_Inspector).Activate += new Outlook.InspectorEvents_ActivateEventHandler(m_Inspector_Activate);
        }
    
        void m_Inspector_Activate()
        {
          if (Application.Inspectors.Count > 0) {
            Debug.WriteLine("\n=== [Test] ================================");
            Debug.WriteLine($" Inspectors.Count: { Application.Inspectors.Count}");
            Debug.WriteLine($" Application.Inspectors: ");
            foreach (Outlook.Inspector CurrentInspector in Application.Inspectors) {
              Debug.WriteLine($"  {CurrentInspector.Caption}");
              Debug.WriteLine($"  --> {CurrentInspector.CurrentItem.Subject}");
            }
            Debug.WriteLine("===========================================\n");
          }
        }
    

    但请注意,Activate-Event 被更频繁地调用。所以每次你 更改或关闭触发事件的 Inspector 窗口。

    【讨论】:

    • 另外请记住,您可以拥有多个检查器,因此拥有一个包装器列表(每个将 Inspector 对象保存在成员变量中并提供事件处理程序)会更合适。您也可以在激活事件处理程序触发一次后断开连接。
    • 嗯,我试图学习包装器的东西,但不幸的是,MS 决定删除所有代码示例。网络上有这些样本的来源吗?
    • 没有太多内容 - 创建一个将 Inspector 对象作为其成员之一并将` Inspector.Activate` 事件处理程序作为其方法之一的类。当NewInspector 事件触发时,创建包装器并将其添加到包装器列表中。当Activate 触发时,您将知道它对应于哪个Inspector 对象。当Close 事件触发时,您可以移除列表的包装器。
    【解决方案2】:

    NewInspector 事件处理程序可能为时过早 - 尝试连接 Inspector.Activate 事件:它在 Inspector 可见时触发。

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 2022-08-23
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2014-08-27
      • 2011-12-12
      相关资源
      最近更新 更多