This documentation is archived and is not being maintained.

Workbook.CommandBars Property

Gets a Microsoft.Office.Core.CommandBars object that represents the Microsoft Office Excel command bars.

Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel (in microsoft.office.tools.excel.dll)

public virtual CommandBars CommandBars { get; }

Property Value

A Microsoft.Office.Core.CommandBars object that represents the Microsoft Office Excel command bars.

When a workbook is embedded in another application and activated by the user by double-clicking the workbook, using the property with a Workbook object returns the set of Excel command bars available within the other application. At all other times, using this property with a Workbook object returns a null reference (Nothing in Visual Basic). There is no programmatic way to return the set of command bars attached to a workbook. Command bars are associated with the application and not the workbook. This property exists on the workbook so you can access Excel application command bars when Excel is not the application.

The following code example uses the CommandBars property to delete all custom command bars that are not visible. This example assumes that the current workbook is embedded in another application.

private void WorkbookCommandBars()
{
    if (this.CommandBars != null)
    {
        for (int i = 1; i <= this.CommandBars.Count; i++)
        {
            if (!this.CommandBars[i].BuiltIn && 
                !this.CommandBars[i].Visible)
            {
                this.CommandBars[i].Delete();
            }
        }
    }
    else
    {
        MessageBox.Show("This workbook must be opened in another " +
            "application to use the CommandBars property.");
    }
}

Show: