2

We are using an add-in express region, which loads on the right-hand side of Outlook. In that form region, we have one user control, MyContainer. This in turn contains two controls.

In MyContainer, we need to decide on the visibility of each child control.

A method involving ActiveInspector and ActiveExplorer, is not reliable.

Example

Opening more than one compose window in the main explorer window, I happen to switch between explorer views, which are; Sent, Outbox, etc. I still find the explorer and inspector objects.

I need a good way to ensure that the window in which my region or MyContainer control is loading in is a compose/read or main Explorer (Inbox/Sent/Drafts/Outbox).

I have tried a number of things, but with no success.

Any pointers or suggestions will be really helpful.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
Defry
  • 84
  • 6

3 Answers3

3

Try Application.ActiveWindow (which can be either Explorer or Inspector).

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
3

A simple if/else condition can do the job.

The main problem is when the In-line response from Outlook is being used.

That is available since version 13. So we use an empty try/catch to handle that.

            Outlook.MailItem Email = null;
            Outlook.Inspector actInspector = Outlook.Application.ActiveInspector();
            if (actInspector == null)
            {
                Outlook.Explorer explorer = Outlook.Application.ActiveExplorer();

                try
                {
                    Email = explorer.GetType().InvokeMember("ActiveInlineResponse", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance |
                            System.Reflection.BindingFlags.Public, null, explorer, null) as Outlook.MailItem;
                }
                finally
                {
                    Marshal.ReleaseComObject(explorer);
                }
            }
            else
            {
                try
                {
                    Email = actInspector.CurrentItem as Outlook.MailItem;
                }
                finally
                {
                    if (actInspector != null) Marshal.ReleaseComObject(actInspector);
                }
            }
Anderson Rissardi
  • 2,377
  • 1
  • 16
  • 21
0

Below code can be useful:

Outlook.MailItem mailItem = null; 
var windowType = Globals.ThisAddIn.Application.ActiveWindow(); 
if (windowType is Outlook.Explorer) 
{    
    // Main Explorer
    Outlook.Explorer explorer = windowType as Outlook.Explorer;    
    mailItem = explorer.Selection[0] as Outlook.MailItem; 
}
else if (windowType is Outlook.Inspector) 
{   
    // Read or Compose
    Outlook.Inspector inspector = windowType as Outlook.Inspector;  
    mailItem = inspector.CurrentItem as Outlook.MailItem; 
}
Himalaya Garg
  • 1,525
  • 18
  • 23