5
votes

I'm new to Outlook programming so for the first step (more advanced later) is the following task:

  • Create a new ribbon (Ribbon1) tab (should be shown in explorers and new inspectors)
  • When selecting (Explore) or opening (Inspector) a mail the ribbon tab should show the subject of the mail (As mentioned to test)

The Explorer part was quite easy to get going but I have problems doing the above for new inspectors

I've tried various approaches but none seem to work:

  • I've tried in the NewInspector to reference the Globals.Ribbons[inspector].Ribbon1 but that is always null

  • I've tried in the NewInspector to subscribe to the Activate event and do it from there but in there the Application.ActiveInspector() is null and if I store a global copy of the inspector from the NewInspector event it does not Work on the first new inspector (only after the second and on on read mails. not on compose mail)

  • I've tried to use Ribbon Load event but that only happens the first time

  • I've tried to use the Globals.Ribbons.Ribbon1 member but that only Work for the first time

(I've found lots of samples of NewInspector but no one seem to want to modify the ribbon other than this one https://stackguides.com/questions/7852017/outlook-2007-ribbon-object-reference-not-set-to-an-instance-of-an-object and for that there are no answers)

This is driving me crazy... Is there really no way to do such a simple task!?

Sample code for reference

    void InspectorsNewInspector(Outlook.Inspector inspector)
    {
        AddInspectorEventHandlers(inspector);
    }

    private void AddInspectorEventHandlers(Outlook.Inspector inspector)
    {
        if (inspector == null)
        {
            return;
        }

        Ribbon1 ribbon1 = Globals.Ribbons[inspector].Ribbon1; //This always return null!!!

        ((Outlook.InspectorEvents_10_Event)inspector).Activate += InspectorActivate;
        ((Outlook.InspectorEvents_10_Event)inspector).Close += InspectorClose;

        _openInspectors.Add(inspector);
    }
2
Share your source for how you wire up InspectorsNewInspector. The entire AddIn class will be beneficial for context.SliverNinja - MSFT

2 Answers

0
votes

During the 'NewInspector' event, Your custom ribbon it not loaded. If you debug the code, You will see that it is only after the 'NewInspector' event that it goes to the custom ribbons designer. What you can do is to implement what you need during the custom ribbon load event. That is

private void MyRibbon_Load(object sender, RibbonUIEventArgs e){}

At here you can get the current active inspector. And from that you can get the item you required.

Ex :

        Inspector inspector = Globals.WPTAddIn.Application.ActiveInspector();
        if (inspector != null)
        {
            if (inspector.CurrentItem != null)
            {
                if (inspector.CurrentItem is MeetingItem)
                {
                    MeetingItem meetingItem = (MeetingItem)inspector.CurrentItem;}}}
0
votes

I've run into the same problem today and came up with a workaround. I've basically been trying to access the Ribbon of a MailItem I just created.

To workaround it I've looped over each of the ribbons in Globals.Ribbons (without using the inspector indexer since that gives you a null reference). With each Ribbon object, I check it's context which is of type Inspector. I can then do an equality check on the Inspector.CurrentItem to determine whether their references are equal. If they are equal I know to use that Ribbon

Here's a snippet

Outlook.MailItem mail = (Outlook.MailItem)this.Application.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector currentInspector = (Outlook.Inspector)mail.GetInspector;

Ribbon2 currentRibbon = null;
foreach (Ribbon2 ribbon in Globals.Ribbons)
{
    var ribbonInspector = (Outlook.Inspector)ribbon.Context;
    if (ribbonInspector.CurrentItem.Equals(currentInspector.CurrentItem))
    {
        currentRibbon = (Ribbon2)ribbon;
        break;
    }
}