Mailitem for Form region

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

Mailitem for Form region
 
BerndH




Posts: 99
Joined: 2014-09-23
I have a formregion, which is shown on some mails with "special features". On this region is a button for showing me the special headers from the mail in a dialogform.

Now I have some problems to get the current underlying mailitem, if I press the button.

the region is also shown in the preview (bottom) in the maillist. If I open a second mail in an external window, switch back the the explorerlist, select another mail and press the button. I don?t get the header from the correct mailitem.

On buttonpress I try to get the actual mailitem like this:

if activeinspector != null
mailitem = activeinspector.currentitem
if activeinspector==null
mailitem = activeexplorer.selection[1] as mailitem

but thats not correct, if I switch back (with open inspector) to the explorer, where I try to click button without fire selectionevents it takes the activeinspector as source.

So how can I get "the mailitem my region is shown for"?

Hope I clearly explained it ;-)

Bernd
Posted 03 Jun, 2022 04:26:25 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Bernd,

The code fragment below is from section Determining a Ribbon control's context; see the PDF file in the folder {Add-in Express}\Docs on your development PC. Although it doesn't apply to your case directly, still it demonstrates the idea:

private void OnClick(object sender, IRibbonControl control, bool pressed) {
    object context = control.Context; if (context == null) return;
    if (context is Outlook.Explorer) {
        Outlook.Explorer explorer = context as Outlook.Explorer;
        Outlook.Selection selection = null;
        try {
            selection = explorer.Selection;
        } catch (Exception ex) { }
        if (selection != null) {
            if (selection.Count > 0) {
                object item = selection[1];
                if (item is Outlook.MailItem) {
                    Outlook.MailItem mail = item as Outlook.MailItem;
                    // process mail
                }
                Marshal.ReleaseComObject(item); item = null;
            }
            Marshal.ReleaseComObject(selection); selection = null;
        }
    } else if (context is Outlook.Inspector) {
        Outlook.Inspector inspector = context as Outlook.Inspector;
        object item = inspector.CurrentItem;
        if (item is Outlook.MailItem) {
            Outlook.MailItem mail = item as Outlook.MailItem;
            // process mail
        }
        Marshal.ReleaseComObject(item); item = null;


In your case you replace getting the context object with calling Outlook.Application.ActiveWindow. The remaining part is shown above: you check whether the active window is an explorer or inspector object and handle it accordingly.

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 03 Jun, 2022 08:51:42 Top
BerndH




Posts: 99
Joined: 2014-09-23
Yes, that worked for me :-)

Thank you
Bernd
Posted 09 Jun, 2022 05:24:59 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
That's great!

Regards from Poland (CEST),

Andrei Smolin
Add-in Express Team Leader
Posted 09 Jun, 2022 05:51:30 Top