Is it possible to configure an Excel 2010 ribbon from a C/C++ XLL using only WIN32 and COM calls? Or can I only configure the old-style command bar? I can't find any good examples.
Is it possible to configure an Excel 2010 ribbon from a C/C++ XLL using only WIN32 and COM calls? Or can I only configure the old-style command bar? I can't find any good examples.
No there is no way to modify the Excel 2010 ribbon from a pure XLL, to do it you have to communicate with Excel via the COM (Component Object Model) IDTExtensibility2 interface (VSTO is based on it).
So if you want to modify the ribbon from C/C++, you need to create a COM addin (using the Active Template Library for instance) and it has different loading scheme (XLL is a standard DLL whereas COM uses a registering mechanism based on the Windows registry).
As you said, it is possible to customise the toolbar from XLL but it is very old-fashioned (and rather undocumented...).
This is fairly straightforward with xlOil (disclaimer: I wrote it). xlOil can register and load a COM add-in on-the-fly from an XLL to give you control of the ribbon. The relevant docs are here. The following code registers an XLL add-in with a ribbon:
#include <xlOil/xlOil.h>
using namespace xloil;
// Callback handler, e.g. for onAction
void ribbonHandler(const RibbonControl& ctrl) {}
struct MyAddin
{
std::shared_ptr<IComAddin> theComAddin;
MyAddin()
{
xllOpenComCall([this]()
{
// Function which takes a callback name used in the ribbon XML
// e.g. "myButton_Action" and returns a suitable callback handler
auto mapper = [](const wchar_t* name) mutable
{
return ribbonHandler;
};
theComAddin = makeAddinWithRibbon(
"Addin Name",
string_containing_ribbon_xml,
mapper);
});
}
};
XLO_DECLARE_ADDIN(MyAddin);