XLL+ Class Library (7.0)

Toolbars

Adding a toolbar to Excel's main window

You can add your own toolbars to Excel, by using the methods of the CXlToolbar class.

Using the AppWizard

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

A new static member object, m_pszToolbarName is added to the application class, as shown below:

CopyC++
class CToolbarDemo1App : public CXllApp
{
public:
    CToolbarDemo1App();

// Names 
public:
    static LPCTSTR m_pszDefName;

...

// Toolbar 
    static LPCTSTR m_pszToolbarName;

...
};

The toolbar name is set in the main cpp file, as follows:

CopyC++
///////////////////////////////////////////////////////////////////////////// 
// The one and only CToolbarDemo1App object

CMenuDemo1App theApp;

/* static */ LPCTSTR CToolbarDemo1App::m_pszDefName = _T("New Xll");
/* static */ LPCTSTR CToolbarDemo1App::m_pszToolbarName = _T("CToolbarDemo1");

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

CopyC++
BOOL CToolbarDemo1App::OnXllOpenEx()
{
    // Create toolbar 
    // TODO: Change the toolbar name and captions 
    //       Write an add-in function to be called when the tool button is clicked 
    //       Add other buttons
    CXlToolbarState tstate;
    CXlToolbar::AddToolbar(m_pszToolbarName, tstate);
    CXlToolbar::AddTool(m_pszToolbarName, 1, _T("MyAddinFunction"), _T("My addin function"));
    CXlToolbar::SetToolBitmap(m_pszToolbarName, 1, IDB_BITMAP1);
    CXlToolbar::ShowToolbar(m_pszToolbarName, tstate, true, CXlToolbar::DockRight);

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

The toolbar is destroyed in OnXllClose(), thus:

CopyC++
void CToolbarDemo1App::OnXllClose()
{
    // Delete toolbar
    CXlToolbar::DeleteToolbar(m_pszToolbarName);

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

You will need to change the code as follows:

  1. Change the toolbar name from "CToolbarDemo1" to something appropriate.
  2. Change the tool's help text from "My addin function" to something appropriate.
  3. Change the tool's macro function name from "MyAddinFunction" to one of your own.
  4. Implement the macro function (see below).
  5. Edit the default tool button bitmap, which can be found in the resource file.
  6. Add other tool buttons.

Macro functions

An add-in function that is invoked by a toolbar 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 sample applications Simple Toolbar, Simple Toolbar using resources and Dynamic Toolbar for instructions on using CXlToolbar.

Next: Utility classes >>

See Also

Simple toolbar sample | Simple toolbar using resources sample