1

I'm currently trying to develop an Outlook Add-In that extends an already existing function. To get it work, I need to hook into the On-Click-Event of an existing button (I have the IdMso of it), and execute additional code. Is that possible, and if so, how? I'm using C# as programming language.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
saltemohn
  • 39
  • 4

1 Answers1

0

Yes, it is possible to repurpose built-in controls. The commands element in the ribbon XML can do the trick, for example:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
   onLoad="OnLoad" > 
   <commands> 
     <command idMso="FileSave" onAction="mySave" /> 
   </commands> 
   <ribbon startFromScratch="false"> 
     <tabs> 
       <tab id="tab1" label="Repurpose Command Demo" > 
         <group id="group1" label="Demo Group"> 
           <toggleButton id="togglebutton1"  
             imageMso="AcceptInvitation"  
             size="large"  
             label="Alter Built-ins"  
             onAction="changeRepurpose" /> 
         </group> 
       </tab> 
     </tabs> 
   </ribbon> 
</customUI>

This sample creates a reference to the save file functionality in Microsoft Office by using the command element. You can determine that this component includes a reference functionality built into Microsoft Office through its use of the idMso attribute.

For ribbon buttons the callback looks like that:

C#: void OnAction(IRibbonControl control, ref bool CancelDefault)

VBA: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

C++: HRESULT OnAction([in] IRibbonControl *pControl, [in,out] VARIANT _BOOL *fCancelDefault)

Visual Basic: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

And for the toggleButton control:

C#: void OnAction(IRibbonControl control, bool pressed, ref bool cancelDefault)

VBA: Sub OnAction(control As IRibbonControl, pressed As Boolean, byRef cancelDefault)

C++: HRESULT OnAction([in] IRibbonControl *pControl, [in] VARIANT_BOOL *pvarfPressed, [in,out] VARIANT _BOOL *fCancelDefault)

Visual Basic: Sub OnAction(control As IRibbonControl, pressed As Boolean, byRef CancelDefault)

See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information.


By default, if a VSTO Add-in attempts to manipulate the Microsoft Office user interface (UI) and fails, no error message is displayed. However, you can configure Microsoft Office applications to display messages for errors that relate to the UI. You can use these messages to help determine why a custom ribbon does not appear, or why a ribbon appears but no controls appear. See How to: Show Add-in user interface errors for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks for the answer! I have now done it as you wrote, however, it does not really work. It seems that Outlook recognizes the change, since nothing happens when I press the button, but it also doesn't execute my code that I wrote in the mySave method – saltemohn Apr 11 '22 at 17:50
  • Could you be more specific? What command do you need to repurpose and what is your code? – Eugene Astafiev Apr 11 '22 at 17:54
  • I want to perform custom code, if DirectRepliesTo is clicked. It should open the window and perform the normal tasks (enable the checkbox and fill it with the current email address), but it should perform additional code. I modified the xml as you told `` and created the method `public void mySave(Office.IRibbonControl control, ref bool CancelDefault){CancelDefault = false;}` but now nothing happens if I click on the button – saltemohn Apr 11 '22 at 17:59
  • Do you get any ribbon UI errors? When do you return that XML? – Eugene Astafiev Apr 11 '22 at 18:00
  • I do not get any errors. I think that the arguments of the method are wrong, because all other self-added buttons and dropdowns in this XML work without problems. – saltemohn Apr 11 '22 at 18:09
  • Are these controls on the same Outlook window (inspector)? – Eugene Astafiev Apr 11 '22 at 18:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243794/discussion-between-saltemohn-and-eugene-astafiev). – saltemohn Apr 11 '22 at 18:13