Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Cannot Enable or Disable Built-in Commands with a Macro


View products that this article applies to.

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 following error message:
Run-time error '1004':
Application-defined or object-defined error.

↑ Back to the top


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.

↑ Back to the top


Resolution

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

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 command on the File menu appear disabled. This macro performs the following steps:
  1. The built-in menu command location is saved.
  2. The built-in menu command is deleted.
  3. A custom menu command with the same caption and index is added to the menu.
  4. The custom menu command is disabled.

Sample Macro

Sub Disable_Builtin_MenuItem()
   Dim i
   Dim x
      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.

↑ Back to the top


References

For additional information about getting help with Visual Basic forApplications, click the article number below to view the article in the Microsoft Knowledge Base:
226118 OFF2000: Programming Resources for Visual Basic for Applications

↑ Back to the top


Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

↑ Back to the top


Article Info
Article ID : 213767
Revision : 1
Created on : 1/1/0001
Published on : 1/1/0001
Exists online : False
Views : 280