XLL+ Class Library

Menus

Adding a menu to Excel's main menu

You can add your own menus to Excel, by creating CXlMenu objects within your application.

Using the AppWizard

The easiest way to add a menu to your application is to check the "Add a menu" check-box in the XLL+ Application Wizard. This will add code to your application, which creates a menu during OnXllOpenEx() and destroys it during OnXllClose().

A new member object, m_menu is added to the application class, as follows:

class CMenuDemo1App : public CXllApp
{
public:
    CMenuDemo1App();

// Names
public:
    static LPCSTR m_pszDefName;

// Menu
    CXlMenu m_menu;

...
};

The menu object is initialized and populated in the OnXllOpenEx() method:

BOOL CMenuDemo1App::OnXllOpenEx()
{
    // Set up menu
    // TODO: Change the menu captions
    //       Write an add-in function to be called when the menu item is clicked
    //       Add other menu items
    m_menu.SetTexts("&My menu");
    m_menu.AddItem("&My menu task", "MyAddinFunction");
    m_menu.Create();

    // TODO: Allocate any application-level resources
    return TRUE;
}

The menu is destroyed in OnXllClose(), thus:

void CMenuDemo1App::OnXllClose()
{
    // Delete menu
    m_menu.Destroy();

    // TODO: Clean up any application-level resources
}

You will need to change the code as follows:

  1. Change the menu caption from "My menu" to something appropriate.
  2. Change the menu item text from "My menu task" to something appropriate.
  3. Change the menu item's macro function name to one of your own.
  4. Implement the macro function (see below).
  5. Add other menu items.

Macro functions

An add-in function that is invoked by a menu should have the following characteristics.

  1. It should be marked as a macro function in the XLL+ Function Wizard.
  2. It should take no arguments.

The macro function can interact with the user through dialogs if required, and can interact with Excel through the methods of CXlMacros and other objects.

See Macro functions for more information.

Example code

Look at the Simple menus and Advanced menus samples for instructions on using CXlMenu.

International support

Menu texts can be localized, so that they appear in the language of the user. See Internationalization : Commands for more information, the International sample for an example of a localized menu.

Next: Toolbars >>