Note: Since XLL+ version 4.3.1, you can use the XLL+ AppWizard to create a new add-in with a menu. See the AppWizard reference for details. However, the instructions below are useful if you want to add a menu to an existing add-in project.
To make your add-in methods available as commands from the Excel Application menu, you must create a CXlMenu object and manage its lifetime. The following walkthrough shows how to create and manage Excel menus from an XLL.
To create the project using Visual Studio 6
To create the project using Visual Studio .NET or Visual Studio 2005
For more details about creating projects, see Creating an add-in project in the XLL+ User Guide.
Adding the menu code
#include <xllplus.h> #include <xlmenu.h>
class CMenuTestApp : public CXllApp { public: CMenuTestApp(); // User-defined menu CXlMenu m_menu; ... };
class CMenuTestApp : public CXllApp { public: CMenuTestApp(); // User-defined menu CXlMenu m_menu; ... // Overrides virtual BOOL OnXllOpenEx(); virtual void OnXllClose(); ... };
BOOL CMenuTestApp::OnXllOpenEx() { // Initialise the menu's text m_menu.SetTexts("&MyMenu", "My add-in menu"); // Add two menu items m_menu.AddItem("Macro &1", "MyAddinMacro1", "Add-in Macro 1"); m_menu.AddItem("Macro &2", "MyAddinMacro2", "Add-in Macro 2"); // Create the menu, to the left of the existing Tools menu m_menu.Create("&Tools"); return TRUE; }
void CMenuTestApp::OnXllClose() { // Destroy the added menu. m_menu.Destroy(); }
Implementing the command functions
It is essential that any functions called from menus be marked as macro functions. This means that they must satisfy two criteria:
The function should take no arguments.
There is no way to pass any arguments to the macro if it is called from a menu.
The Macro function check-box should be checked in the XLL+ Function wizard.
This will ensure that the final argument to the IMPLEMENT_XLLFN2() macro is 2, as in the example functions below, and thereby make the function available from menus and push-buttons.
// Function: MyAddinMacro1 // Purpose: Demo macro function //{{XLP_SRC(MyAddinMacro1) // NOTE - the FunctionWizard will add and remove mapping code here. // DO NOT EDIT what you see in these blocks of generated code! IMPLEMENT_XLLFN2(MyAddinMacro1, "R", "MyAddinMacro1", "", "User Defined", "Demo macro function", "", "", 2) extern "C" __declspec( dllexport ) LPXLOPER MyAddinMacro1() { CXlOper xloResult; //}}XLP_SRC MessageBox(CXllApp::XlHwnd(), "Add-in macro 1", "Add-in macro", 0); return xloResult.Ret(); } // Function: MyAddinMacro2 // Purpose: Demo macro function //{{XLP_SRC(MyAddinMacro2) // NOTE - the FunctionWizard will add and remove mapping code here. // DO NOT EDIT what you see in these blocks of generated code! IMPLEMENT_XLLFN2(MyAddinMacro2, "R", "MyAddinMacro2", "", "User Defined", "Demo macro function", "", "", 2) extern "C" __declspec( dllexport ) LPXLOPER MyAddinMacro2() { CXlOper xloResult; //}}XLP_SRC MessageBox(CXllApp::XlHwnd(), "Add-in macro 2", "Add-in macro", 0); return xloResult.Ret(); }
To register the add-in
To unregister the add-in