Cannot Enable or Disable Built-in Commands with a Macro (160903)
The information in this article applies to:
- Microsoft Excel 2000
- Microsoft Excel 97 for Windows
- Microsoft Excel for Windows 95
- Microsoft Excel for Windows 5.0
- Microsoft Excel 98 Macintosh Edition
This article was previously published under Q160903 SYMPTOMS
When you run a macro that attempts to set the Enabled property for a
built-in menu command or a submenu command, you receive an error message.
In Microsoft Excel 97 and Microsoft Excel 98 Macintosh Edition, you
receive the following error message:
Run-time error '1004':
Application-defined or object-defined error
In Microsoft Excel 5.0 and 7.0, you receive the following error message:
Run-time error '1005':
Cannot enable built-in commands.
CAUSE
In Microsoft Excel, you cannot enable or disable built-in menu commands and
submenu commands. You can only set the Enabled property for a menu command
that you add to a menu.
RESOLUTIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. Simulating Disabling a Built-in Menu Command
You cannot enable or disable a built-in menu command. However, you can
create a macro that gives a built-in menu command the appearance of being
disabled.
The sample macro in this article makes the built-in Exit menu command
on the File menu appear disabled. This macro performs the following steps:
- The built-in menu command location is saved.
- The built-in menu command is deleted.
- A custom menu command with the same caption and index is added to the
menu.
- The custom menu command is disabled.
Sample Macro
Sub Disable_Builtin_MenuItem()
Dim i
Dim x
'If you are using Microsoft Excel on the Macintosh replace
'the word "Exit" with "Quit" on the following line
With MenuBars(xlWorksheet).Menus("File").MenuItems("Exit")
i = .Index 'Save the position of the built-in menu command.
.Delete 'Delete the built-in menu command.
End With
'Add a custom menu command with the same caption and index as the
'built-in menu command that you deleted.
Set x = MenuBars(xlWorksheet).Menus("File").MenuItems.Add _
(Caption:="Exit", before:=i)
x.Enabled = False
End Sub
Resetting the Menu That Is Changed by the Sample Macro
To reset the entire menu to the default values, run the following macro:
Sub ResetMenubar()
MenuBars(xlWorksheet).Reset
End Sub
NOTE: There is not a way to reset a single menu or menu command.
REFERENCES
For additional information about getting help with Visual Basic for
Applications, please see the following article in the Microsoft Knowledge
Base:
163435 VBA: Programming Resources for Visual Basic for Applications
226118 OFF2000: Programming Resources for Visual Basic for Applications
Modification Type: | Minor | Last Reviewed: | 10/11/2006 |
---|
Keywords: | kbdtacode kbprb kbProgramming kbui KB160903 |
---|
|