C#, VSTO, Outlook 2016.
I want to get a list of the titles of all opened Inspectors in Outlook.
The first call of Outlook.Application.Inspectors[1].Caption returns "Message" as the inspectors caption. But the window-title of the inspector shows the subject of the email.
But when I open more inspectors then the caption seems to change.
So there is always one inspector in the collection without the emails subject and the Inspector.Caption doesn't match the coresponding window-title.
So how could I get a list of the right window-titles?
Coresponding code:
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");
}
}
}
NewInspector
事件处理程序可能为时过早 - 尝试连接Inspector.Activate
事件:它在 Inspector 可见时触发。
Thank you Dmitry Streblechenko, this does the job:
For reference:
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");
}
}
But be aware of that the Activate-Event is called more often. So every time you change or close an Inspector-window the event is fired.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.