views:

705

answers:

2

Is it possible to access the ActiveInspector at the time of ribbon load. Application.ActiveInspector() returns proper value when I use custom form but does not for default contact form.

I need the ActiveInspector to customize ribbon button depending on a property value in ActiveInspector().CurrentItem.

A: 

Can you handle the NewInspector event of the Inspectors collection, then hold onto that in a static field (probably should use a WeakReference) that you can access from the ribbon load event?

I haven't done any Ribbon customization with Outlook but I've done extensive work with inspectors in my old Tablet PC product, TEO. They're a pain in the ass to work with but the main thing you don't wanna do is hang onto an Inspector reference too long because you'll get all kinds of weird problems with shutting down Outlook or canceling pending edits.

Josh Einstein
A: 

Hi, I do a slight variation on the above in that I maintain list of the open inspectors (I wrap the inspector an keep that in the list ). Adding them on the new inspector event and removing them on a wired up close event close.

In my ribbon code I have a static method FindOutlookInspector that finds the inspector using the control.context of the ribbon.

Something along these lines .. OutlookInspector is my wrapped inspector class but you may not need that etc. Also I only care about Mail Items

 void _inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            OutlookItem olItem = null;
            try
            {
                object newitem = Inspector.CurrentItem;
                olItem = new OutlookItem(newitem);
                 if (olItem.Class == Outlook.OlObjectClass.olMail && olItem.MessageClass == "IPM.Note")
                 {

                         OutlookInspector existingWindow = FindOutlookInspector(Inspector);

                         if (existingWindow == null)
                         {
                             OutlookInspector window = new OutlookInspector(Inspector);

                             window.Close += new EventHandler(WrappedWindow_Close);
                             _windows.Add(window);
                         }

            }
            catch (Exception ex)
            {
                throw ex;
            }



        }

        void WrappedWindow_Close(object sender, EventArgs e)
        {
            OutlookInspector window = (OutlookInspector)sender;
            window.Close -= WrappedWindow_Close;
            _windows.Remove(window);

        }


        internal static OutlookInspector FindOutlookInspector(object window)
        {


                foreach (OutlookInspector inspector in _windows)
                {
                    if (inspector.Window == window)
                    {
                        return inspector;
                    }
                }
                return null;





        }

Then in the ribbon code i can call FindOutlookInspector to get the wrapped inspector

 OutlookInspector window = ThisAddIn.FindOutlookInspector(control.Context);
76mel
@76mel Make sure to explicitly decreased a the COM references of Item objects. YMMV.
pst