您的位置:首页 > 其它

Disable Command bars and controls in Excel 97-2003

2009-10-05 16:24 645 查看
Disable Command bars and controls in Excel 97-2003

Ron de Bruin (last update 30-Oct-2007)
Go back to the Excel tips page

Important: All examples on this page use False to disable the commandbar/toolbar/control.
Change it to True if you want to enable the control.

Download: Example workbook with the code from this page and more examples

How do I disable a Command bar
How do I disable all Command bars
How do I disable a Menu or Control
Screenshots Worksheet Menu Bar Excel 2003 with ID numbers
Other useful examples
Helpful Links

How do I disable a Command bar

This code line will disable the Worksheet Menu Bar
Application.CommandBars("Worksheet Menu Bar").Enabled = False

This code line will disable the Standard toolbar
Application.CommandBars("Standard").Enabled = False

This code line will disable the Cell menu (menu when you right click on a cell)
Application.CommandBars("Cell").Enabled = False

Important: you must always use the English name of the Command bar in the code.
If you use the local name of the Command bar it is not working.

If you don't know the English name for a Command Bar you can run this macro.
It will add a new worksheet to your workbook with a list of all names (English and local).
Sub Get_Commandbars_Names()
Dim Cbar As CommandBar
Dim NewWS As Worksheet
Dim RNum As Long

RNum = 1
Set NewWS = Worksheets.Add
On Error Resume Next
ActiveSheet.Name = "CommandBarNames"
On Error GoTo 0

For Each Cbar In Application.CommandBars
NewWS.Cells(RNum, "A").Value = Cbar.Name
NewWS.Cells(RNum, "B").Value = Cbar.NameLocal
RNum = RNum + 1
Next Cbar

NewWS.Columns.AutoFit
End Sub


How do I disable all Command bars

If you want them back use True instead of False in the code
Sub Disable_Command_Bars_1()
'This will disable all Command bars
Dim Cbar As CommandBar
For Each Cbar In Application.CommandBars
Cbar.Enabled = False
Next
End Sub
More examples are in the workbook that you can download on this page.

How do I disable a Menu or Control

You see that you must use the Id's of the menu/controls when you have read
this part of the page. But how do you find them ?

You can use this Add-in (CommandBar Tools)
http://www.erlandsendata.no/english/index.php?d=endownloadcommandbars

See also the screenshots below this section of the webpage with
all menus from the worksheet Menu Bar in Excel 2003.

Disable a menu on the Worksheet Menu Bar

Both Examples will disable the File menu in the Worksheet Menu Bar

1) Use the name of the command bar control



Application.CommandBars("Worksheet Menu Bar").Controls("File").Enabled = False

But this is not working if you run the code in a Excel version that is not English.
File is Bestand in the Dutch Excel version for Example.

2) Use the ID number of the command bar control ( working in every Excel language version)



Application.CommandBars("Worksheet Menu Bar").FindControl(ID:=30002).Enabled = False

because there is only one control with the ID 30002 you can use this line also because
FindControl will find the first occurrence of the ID.

Application.CommandBars.FindControl(ID:=30002).Enabled = False

Disable a control

This code line disable View - Full Screen in the Worksheet Menu Bar
Application.CommandBars("Worksheet Menu Bar").FindControl _
(ID:=178, Recursive:=True).Enabled = False

I use Recursive:=True in this code example to look in all sub menus in the Worksheet Menu Bar

This example will only disable the "Copy" control on the Standard Toolbar.
Application.CommandBars("Standard").FindControl(ID:=19).Enabled = False

The examples below disable all the "Copy" controls in all command bars.
Note: Every "Copy" control in the Menu Bar, Toolbars and right click menus

If you use Excel 2000-2003 you can use the code below that use the Office.CommandBarControl
Sub MenuControl_False()
' Excel 2000 - 2003
Dim Ctrl As Office.CommandBarControl
For Each Ctrl In Application.CommandBars.FindControls(ID:=19)
Ctrl.Enabled = False
Next Ctrl
End Sub
If you use Excel 97 then use this example (also working in 2000-2003)
Sub MenuControl_Enabled_False()
' Excel 97 - 2003
Dim Ctl As CommandBarControl
Dim Cbar As Integer

On Error Resume Next
For Cbar = 1 To Application.CommandBars.Count
For Each Ctl In Application.CommandBars(Cbar).Controls
Application.CommandBars(Cbar).FindControl(ID:=19, _
Recursive:=True).Enabled = False
Next Ctl
Next Cbar
On Error GoTo 0
End Sub

Screenshots Worksheet Menu Bar Excel 2003 with ID numbers

Check out also this Add-in (CommandBar Tools) to find all ID numbers
for all commandbars in Excel.
http://www.erlandsendata.no/english/index.php?d=endownloadcommandbars























Other useful examples

1) How do I run the code automatic when I activate and deactivate a workbook

You can use this events in the ThisWorkbook module.

Read more about events on Chip Pearson's site.
http://www.cpearson.com/excel/events.htm

Private Sub Workbook_Activate()
'Your code to change Enabled to False
'Or the name of your macro
End Sub

Private Sub Workbook_Deactivate()
'Your code to change Enabled to True
'Or the name of your macro
End Sub

Or use this two events

Private Sub Workbook_Open()
'Your code to change Enabled to False
'Or the name of your macro
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Your code to change Enabled to False
'Or the name of your macro
End Sub

2) Hide the FormulaBar and StatusBar

Application.DisplayFormulaBar = False
Application.DisplayStatusBar = False

3) Disable the shortcuts

You can use Application.OnKey to do this.
See the VBA help for more information.

Example to disable Ctrl c (the Copy shortcut)

Application.OnKey "^c", ""

to restore use

Application.OnKey "^c"

4) Other

Disable right clicking on the worksheet menu bar and other bars in 97 SR1 and above.

On Error Resume Next
Application.CommandBars("Toolbar List").Enabled = False
On Error GoTo 0

This prevents users double-clicking on the Toolbar area to open the
(Customize Toolbars) dialog in Excel 2002 and above.
Application.CommandBars.DisableCustomize = True

If you want to remove “Type a question for Help” on the Worksheet Menu Bar you
can use this in Excel 2002 and above. True = hidden and False = visible
Application.CommandBars.DisableAskAQuestionDropdown = True

You can use this to avoid the error in Excel 2000

If Val(Application.Version) > 9 Then
CallByName CommandBars, "DisableCustomize", VbLet, True
CallByName CommandBars, "DisableAskAQuestionDropdown", VbLet, True
End If

5) Reset a commandbar

Application.CommandBars("Worksheet Menu Bar").Reset

Resetting a built-in control restores the actions originally intended for the control and resets
each of the control's properties back to its original state.
Resetting a built-in command bar removes custom controls and restores built-in controls.

Links

http://support.microsoft.com/default.aspx?scid=kb;en-us;830502&Product=xlw
How to customize menus and menu bars in Excel

http://support.microsoft.com/default.aspx?scid=kb;EN-US;213552
XL2000: List of ID Numbers for Built-In CommandBar Controls

http://support.microsoft.com/default.aspx?scid=kb;en-us;162814
Sample Macros to Return ID for a CommandBar Control

http://support.microsoft.com/default.aspx?scid=kb;en-us;213209&Product=xlw
XL2000: Sample Macros that Customize and Control Shortcut Menus

http://support.microsoft.com/default.aspx?scid=kb;en-us;213550&Product=xlw
XL2000: Sample Macros for Customizing Menus and Submenus
---------------------------------------------------------------------------------------
转自http://www.rondebruin.nl/menuid.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: