You can add your own toolbars to Excel, by using the methods of the CXlToolbar class.
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:
class CToolbarDemo1App : public CXllApp { public: CToolbarDemo1App(); // Names public: static LPCSTR m_pszDefName; ... // Toolbar static LPCSTR m_pszToolbarName; ... };
The toolbar name is set in the main cpp file, as follows:
///////////////////////////////////////////////////////////////////////////// // The one and only CToolbarDemo1App object CMenuDemo1App theApp; /* static */ LPCSTR CToolbarDemo1App::m_pszDefName = _T("New Xll"); /* static */ LPCSTR CToolbarDemo1App::m_pszToolbarName = _T("CToolbarDemo1");
The toolbar is initialized and populated in the OnXllOpenEx() method:
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, "MyAddinFunction", "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:
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:
An add-in function that is invoked by a toolbar should have the following characteristics.
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.
Look at the sample applications Simple Toolbar, Simple Toolbar using resources Dynamic Toolbar for instructions on using CXlToolbar.