What is needed: I need an Excel addin which can capture the click events of various Standard Command bar buttons on the Ribbon tabs. Also, once this is done the normal processing of the command button should proceed.
Example : When I capture ribbon button click event, when the "Save" button is clicked , I should be able to a) Do custom processing, display a message box or log this click event to a flat file AND b) Let excel do the actual work of saving the worksheet
I have referred -
Temporarily Repurpose Commands on the Office Fluent Ribbon
Customizing the 2007 Office Fluent Ribbon for Developers
Although, am able to capture the standard button clicks am not able to get both points a) and b) to work at the same time.
Ribbon1.xml
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<commands>
<command idMso="FileSave" onAction="OnRibbonButtonClick" />
</commands>
</customUI>
Ribbon1.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
namespace ExcelAddIn
{
[ComVisible(true)]
public class Ribbon1 : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public Ribbon1()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("ExcelAddIn.Ribbon1.xml");
}
#endregion
#region Ribbon Callbacks
//Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
}
public void OnRibbonButtonClick(Office.IRibbonControl control, bool Cancel)
//public void OnRibbonButtonClick(Office.IRibbonControl control)
{
Globals.ThisAddIn.OnStandardRibbonCommand(control.Id);
ribbon.Invalidate();
Cancel = false;
}
#endregion
#region Helpers
private static string GetResourceText(string resourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] resourceNames = asm.GetManifestResourceNames();
for (int i = 0; i < resourceNames.Length; ++i)
{
if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
{
if (resourceReader != null)
{
return resourceReader.ReadToEnd();
}
}
}
}
return null;
}
#endregion
}
}
With the above source code, I can capture the FileSave event, but the file does not get saved. i.e. the default Excel funtionality of saving the file does not happen.
Am I missing something or this cannot be done?
[Temporarily Repurpose Commands on the Office Fluent Ribbon] Does this mean that the command is completely repurposed and the original purpose cannot be fulfilled?
Environment: Microsoft® Excel® for Microsoft 365 MSO (Version 2205 Build 16.0.15225.20172) 64-bit
Microsoft Visual Studio Enterprise 2019 Version 16.11.7
VSTO version 10.0.60724
The repurposed callback should have the following signature:
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)
It seems you need to use the following method instead:
public void OnRibbonButtonClick(Office.IRibbonControl control, ref bool Cancel)
{
Debug.Print(control.Id);
Cancel = false;
}