XL97: Error Adding or Deleting a Menu Command in a Macro

Last reviewed: March 20, 1998
Article ID: Q160520
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SYMPTOMS

In Microsoft Excel 97, if you run a Visual Basic for Applications macro that adds a command to a menu or deletes a command from a menu, you may receive the following error message:

   Run-time error '1004':
   Application-defined or object-defined error

CAUSE

This problem occurs when, in the macro command to add or delete the menu command, you specify a menu command that no longer exists, or is changed in Microsoft Excel 97.

For a list of the menu commands that are changed in Microsoft Excel 97, see the "More Information" section in this article.

WORKAROUND

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 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/

To work around this behavior, use either of the following methods:
  • If you use the macro only in Microsoft Excel 97, replace the earlier menu command name with the new menu command name in the macro.

        -or-
    
  • If you use the macro in Microsoft Excel 5.0, 7.0, and Microsoft Excel 97, modify the macro. For example, assume that you created the following macro:

          Sub AddTest1()
    
              MenuBars(xlWorksheet).Menus("View").MenuItems.Add _
                  Caption:="Test", Before:="Toolbars..."
    
          End Sub
    
    
Although this macro works correctly in earlier versions of Microsoft Excel, it does not work in Microsoft Excel 97. The macro does not work because the "Toolbars..." command is called Toolbars (no ellipsis) in Microsoft Excel 97.

However, the following macro works correctly in all versions of Microsoft Excel:

      Sub AddTest2()

          'Test whether you use Microsoft Excel 5.0 or 7.0.
          If Val(Application.Version) < 8 Then

              'Set the value of the variable equal to the correct menu
              'command name for Microsoft Excel 5.0 and 7.0.
              xBefore = "Toolbars..."

          'Otherwise, assume you use Microsoft Excel 97 (version 8.0).
          ElseIf Val(Application.Version) = 8 Then

              'Set the value of the variable equal to the correct menu
              'command name for Microsoft Excel 97.
              xBefore = "Toolbars"

          End If

          'Add the new command. The command works correctly because the
          'menu command name in the Before argument (the variable xBefore)
          'is correct.
          MenuBars(xlWorksheet).Menus("View").MenuItems.Add _
              Caption:="Test", Before:=xBefore

      End Sub

STATUS

This behavior is by design of Microsoft Excel 97.

MORE INFORMATION

In Microsoft Excel, in a Visual Basic macro, you can add a new menu command to a menu by using a line of code similar to the following:

   MenuBars(xlWorksheet).Menus("View").MenuItems.Add Caption:="Test", _
       Before:="Toolbars..."

This line of code adds a new menu command, Test, above the "Toolbars..." menu command on the View menu.

You can also delete a menu command by using a line of code similar to the following:

   MenuBars(xlWorksheet).Menus("View").MenuItems("Toolbars...").Delete

In each of the lines of code, the menu command "Toolbars..." is referred by its exact name. In Microsoft Excel 97, the names of some menu commands are changed. The new names may cause problems if macro code refers to these menu items by name.

The two lines of code work correctly in earlier versions of Microsoft Excel. However, they do not work correctly in Microsoft Excel 97, because the menu command "Toolbars..." is renamed Toolbars (no ellipsis).

The following menu commands are changed in Microsoft Excel 97.

                  Microsoft Excel 5.0, 7.0   Microsoft Excel 97
   Menu           menu command               menu command
   --------------------------------------------------------------

   View           Toolbars...                Toolbars
   Insert         Chart                      Chart...
   Insert         Note...                    Comment
   Data           PivotTable...              PivotTable Report...

   sheet tab      Rename...                  Rename
   shortcut
   menu*

* This menu is the xlWorkbookTab shortcut menu.


Additional query words: XL97
Keywords : kberrmsg kbdta kbdtacode xlvbahowto xlvbainfo xlui xl97vbmigrate
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 20, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.