Freigeben über


WorkbookBase.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.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

Syntax

'Declaration
Public ReadOnly Property CommandBars As CommandBars
public CommandBars CommandBars { get; }

Property Value

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

Remarks

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 nulla 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.

Examples

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.

This example is for a document-level customization.

Private Sub WorkbookCommandBars()
    If Not (Me.CommandBars Is Nothing) Then 
        Dim i As Integer 
        For i = 1 To Me.CommandBars.Count
            If Not Me.CommandBars(i).BuiltIn AndAlso Not _
                Me.CommandBars(i).Visible Then 
                Me.CommandBars(i).Delete()
            End If 
        Next i
    Else
        MsgBox("This workbook must be opened in another " & _
            "application to use the CommandBars property.")
    End If 
End Sub
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.");
    }
}

.NET Framework Security

See Also

Reference

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace