none
How to call a custom command form a built-in control of the attachment menu in MS Outlook 2010 RRS feed

  • Question

  • Hello,

    My requirement in detail,

    I need to call a validation routing before an attachment is saved. Therefore when the user right clicks on an email attachment and select the "Save As" option from the attachment context menu, it needs to run a specific routing and if success do the default(prompt the dialog and save the attachments) or if unsuccessful cancel the default.

    Also the "Save As" button in the ribbon attachment tab needs to have the same behavior.

    As the onAction attribute for built in controls of the attachment context menu does not work, the command tag was used as follows in the XML definition,

    <commands>
    	<command idMso="SaveAttachAs" onAction="TestButtonClicked"/>
    </commands>

    And the code as follows,

    public void TestButtonClicked(IRibbonControl cmdBarButton, ref bool cancel)
    {
        if (MessageBox.Show("Cancel?", "Test", MessageBoxButtons.YesNo) == DialogResult.Yes)
            cancel = true;
        else
            cancel = false;
    }

    Now when clicked on the "Save As" of the ribbon attachment tab, it prompts the message and works fine(when not cancelled runs the default).

    But when "Save As" is selected from the attachment context menu the message shows and when not cancelled it does not run the default code(nothing happens). How can I run the default?

    Please help.

    Saturday, March 7, 2015 5:12 AM

Answers

  • There is no other way to get the default action.

    But I don't think that calling the ExecuteMso in the repurposed callback is a good idea because you can call the repurposed callback indefinitely.  Did you try to the ExecuteMso method but not display any dialogs the second time?

    Thursday, March 12, 2015 12:52 PM

All replies

  • Hello Damian,

    I have checked the SaveAttachAs control ID in my Outlook 2013 (x64). And it works like a charm. I always get a message box mentioned in your code.

    It looks like you didn't return the XML markup for the Microsoft.Outlook.Mail.Read value passed to the GetCustomUI method of the IRibbonExtensibility interface. In the code you return the proper markup for the Microsoft.Outlook.Explorer value passed only.

    So, you need to return the same XML markup for the Explorer and Mail.Read contexts:

    <commands>
    	<command idMso="SaveAttachAs" onAction="TestButtonClicked"/>
    </commands>
    See Extending the User Interface in Outlook 2010 for more information.

    Saturday, March 7, 2015 11:01 AM
  • Hello Eugene,

    As suggested, I returned the same XML markup for Microsoft.Outlook.Mail.Read and Microsoft.Outlook.Explorer from GetCustomUI. But still the same problem.

    I may have not been clear in the above problem, so here it is.

    When "Save As" is selected from the attachment context menu, the message box is shown without a problem. Thereafter when ref bool cancel of TestButtonClicked(IRibbonControl cmdBarButton, ref bool cancel) is updated with false, it does not run the default. That means it does not show the Save File dialog.

    Also note that before the mail is opened, that means before GetCustomUI() triggers with Microsoft.Outlook.Mail.Read, the attachment could be saved by the attachment context menu, by right clicking on the attachment.

    Have I missed out anything else? Please advice.

    Monday, March 9, 2015 6:51 AM
  • Damian,

    If you try to leave the only one statement in the code event handler:

    public void TestButtonClicked(IRibbonControl cmdBarButton, ref bool cancel)
    {
        //if (MessageBox.Show("Cancel?", "Test", MessageBoxButtons.YesNo) == DialogResult.Yes)
        //    cancel = true;
        //else
            cancel = false;
    }

    You can see the built-in dialog window. It looks like MessageBox is the cause of the issue. Try to not display modal windows from the event handler.

    Monday, March 9, 2015 2:02 PM
  • Hello Eugene,

    Yes, without the message box it works. But I need the messagebox to get the confirmation from the user and proceed.

    Also I don't understand, it works from the "Save As" button of the attachments Ribbon, but not from the "Save As" button from the attachment Context menu.

    Again, if I cancel the event and call the Application.ActiveExplorer().CommandBars.ExecureMso("SaveAs"), the same event that I coded above triggers, not the base event that MS Outlook calls. Therefore it shows the messagebox repeatedly.

    Alternatively, I figured out the following. Do you have a better suggestion?

    private Microsoft.Office.Interop.Outlook.Application _applicationObject;

    private bool _selecetedSaveAs = false; public void OnMailAttachmentSave(IRibbonControl control, ref bool cancel) { if (_selecetedSaveAs) { cancel = false; _selecetedSaveAs = false; } else { if (MessageBox.Show("Cancel?", "Test", MessageBoxButtons.YesNo) == DialogResult.Yes) { cancel = true; _selecetedSaveAs = false; } else { cancel = true; _selecetedSaveAs = true; Microsoft.Office.Interop.Outlook.Explorer activeExplorer = _applicationObject.ActiveExplorer(); activeExplorer.CommandBars.ExecuteMso("SaveAttachAs"); } } }

    Thanks for your support.
    Tuesday, March 10, 2015 5:35 AM
  • Damian,

    > Yes, without the message box it works. But I need the messagebox to get the confirmation from the user and proceed.

    You can try to display a non-modal form asking whether to save it or not. Hope it will help.

    > Also I don't understand, it works from the "Save As" button of the attachments Ribbon, but not from the "Save As" button from the attachment Context menu.

    Displaying a message box from the event handler hides/closes the context menu where the command is located. Of course, it is just my assumptions.

    > Again, if I cancel the event and call the Application.ActiveExplorer().CommandBars.ExecureMso("SaveAs"), the same event that I coded above triggers, not the base event that MS Outlook calls. Therefore it shows the messagebox repeatedly.

    What base events are you talking about? The ExecuteMso method runs the built-in  controls programmatically. I don't see anything strange in the repurposed controls in that case.

    Tuesday, March 10, 2015 9:47 AM
  • Hello Eugene,

    >You can try to display a non-modal form asking whether to save it or not. Hope it will help.

    I need the user confirmation to proceed. Showing a non-modal form continues the process.

    > Displaying a message box from the event handler hides/closes the context menu where the command is located. Of course, it is just my assumptions.

    Seems to be the case.

    > What base events are you talking about? The ExecuteMso method runs the built-in  controls programmatically. I don't see anything strange in the repurposed controls in that case.

    ExecuteMso() calls the method set in the XML definition. But not the default.

    In the above code sample ExecuteMso("SaveAs") calls the customized OnMailAttachmentSave(), but not the Outlook inbuilt method.

    Anyway I have tried with the above sample and it works as required.

    Thanks.

    Wednesday, March 11, 2015 11:22 AM
  • Hi Damian,

    > ExecuteMso() calls the method set in the XML definition. But not the default.

    It looks like you cancel the default action in the "repurposed" callback. Is that the case?

    Wednesday, March 11, 2015 1:35 PM
  • Hi Eugene,

    Yes, the default was cancelled in "repurposed" callback.

    Is there anyway from within the "repurposed" callback to call the default with ExecuteMso()[or any other way], as updating the cancel with false does not call the default when there is a model window.

    If possible, I could reduce a few code lines from the above sample.

    Thursday, March 12, 2015 3:24 AM
  • There is no other way to get the default action.

    But I don't think that calling the ExecuteMso in the repurposed callback is a good idea because you can call the repurposed callback indefinitely.  Did you try to the ExecuteMso method but not display any dialogs the second time?

    Thursday, March 12, 2015 12:52 PM
  • Hi Eugene,

    >Did you try to the ExecuteMso method but not display any dialogs the second time?

    Yes. Within the repurposed callback for the second time, dialogs are not displayed and default action works.

    Since this is the only solution for now, I'll go with this.

    Thanks again.

    Friday, March 13, 2015 3:34 AM