Access a form when a mail is opened

Add-in Express™ Support Service
That's what is more important than anything else

Access a form when a mail is opened
 
Kristian Benoit




Posts: 16
Joined: 2009-03-30
I have a custom pane that shows both in explorer and inspectors (for mail).

I need to update the content of the pane instance shown in the explorer when the selection changes. And I need to update the content of the one shown in the inspector when the inspector apears (item.Open).

The only way I've found to get the instance of the pane is:

private ADXOlForm CurrentForm
{
    get
    {
        return ((ADXOlFormsCollectionItem)adxOlFormsCollectionItem1).GetCurrentForm(EmbeddedFormStates.Active);
    }
}


To get notified of the mail opening I hook on new Inspector, I also tried InspectorActivate.

adxOutlookEvents_NewInspector(object sender, object inspector, string folderName)
{
     
    Outlook._Inspector insp = (Outlook._Inspector)inspector;
    Outlook.MailItem item = insp.CurrentItem as Outlook.MailItem;
    if (item == null)
    {
        Trace.WriteLine("Not a mail, nothing to do.");
        return;
    }
    item.Open += new Outlook.ItemEvents_OpenEventHandler(item_Open);
}


Then When the mail is opened:

void item_Open(ref bool Cancel)
{
    Outlook._MailItem item = (OutlookApp.ActiveInspector().CurrentItem as Outlook._MailItem);
    ((MyForm)CurrentForm).Update(item);
}


The problem here, is that OutlookApp.ActiveInspector() return null. I tried saving the Inspector on NewInspector as a class property, but since the inspector is not active yet, It update the form in the explorer pane.

My questions are:
How and When do I get access to the instance of the form in the inspector.
Posted 06 Jun, 2009 00:21:07 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Kristian,

You can use the ADXBeforeFormShow event of the ADXOlForm. In this event you'll also need to set a flag indicating that the changes are already done.


Andrei Smolin
Add-in Express Team Leader
Posted 08 Jun, 2009 07:44:23 Top
Kristian Benoit




Posts: 16
Joined: 2009-03-30
Thanks, at first, it seems much better now, but corner cases are still not working.

I realize that the problem I have is associating the my form with a mail Item.

Here's what I have now
 private ExplorerForm = null;
public void BeforeFormShow(ADXOlForm form)
{
    Outlook.Inspector inspector = OutlookApp.ActiveInspector();
    Outlook._MailItem item;
    if (inspector != null)
    {
        item = (Outlook._MailItem)OutlookApp.ActiveInspector().CurrentItem;
        SetFormContentFromMail(item, form);
    }
    else
        ExplorerForm = form;
}

private void ExplorerSelectionChange(object sender, object explorer)
{
    if (ExplorerForm != null)
    {
        Outlook._Explorer olExpl = explorer as Outlook._Explorer;
        Outlook.Selection olSelection = olExpl.Selection;
        if (olSelection.Count == 1)
        {
            SetFormContentFromMail((Outlook._MailItem)olSelection.Item(1),
                     ExplorerForm);
        }
    }
}


Now if I selection 2 mails in the folder list, and right-click/Open Items, the 2 inspectors hosted forms are associated to the same mail. And I'm not sure if that there can be only one explorer. Seems so in MSO 2k3 and 2k7, but dont know for the others and the API (event NewExplorer) suggest that there can be more than one and I'm keeping a reference to the last one that shown one of my form.
Posted 08 Jun, 2009 18:56:43 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Kristian,

When a new inspector or explorer window opens, a new pane instance is embedded to the window and ADXBeforeFormShow event occurs. In this event you can initialize your form as required by your business logics.

To keep references to the pane instances isn't a good idea becuse any pane instance can be removed by the Outlook Forms Manager. We recommend that you use ADXOlFormsCollectionItem.GetCurrentForm to get the pane instance which is currently active. To get all pane instances, use ADXOlFormsCollectionItem.FormInstances and ADXOlFormsCollectionItem.FormInstanceCount.


Andrei Smolin
Add-in Express Team Leader
Posted 09 Jun, 2009 10:38:21 Top
Kristian Benoit




Posts: 16
Joined: 2009-03-30
Thank Andrei, so if I understand correctly to update the form according to the mail associated with it, within the SelectionChange handler, I get the the form through ADXOlFormCollection.GetCurrentForm() and the item through the OutlookApp.ActiveExplorer().CurrentItem.

And to associate them within ADXBeforeFormShow handler, the form is "this", but where should I get the reference to the item associated with the form ? Either the selected one if the form is in an explorer or the the one inspected by an inspector.
Posted 09 Jun, 2009 12:11:34 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Kristian,

ADXOlForm provides the ExplorerObj and InspectorObj properties. One of them will be null. Cast another one to Outlook.Explorer or Outlook.Inspector and then use either Explorer.Selection.Items or Inspector.CurrentItem.


Andrei Smolin
Add-in Express Team Leader
Posted 09 Jun, 2009 12:21:38 Top