XL2000: Error Adding or Deleting a Menu Command in a Macro (213530)
The information in this article applies to:
This article was previously published under Q213530 SYMPTOMS
If you run a Microsoft 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 the current version of Microsoft Excel. This behavior is by design of Microsoft Excel.
For a list of the menu commands that are changed in Microsoft Excel, see
the "More Information" section in this article.
WORKAROUNDMicrosoft 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.
To work around this behavior, use either of the following methods:
Although this macro works correctly in versions of Microsoft
Excel earlier than Excel 97, it does not work in Microsoft Excel 2000. The macro does not work because the Toolbars... command is called Toolbars (no ellipsis) in Microsoft Excel 97 and later.
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)
'or Microsoft Excel 2000 (version 9.0).
ElseIf Val(Application.Version) >= 8 Then
'Set the value of the variable equal to the correct menu
'command name for Microsoft Excel 97
'and Microsoft Excel 2000.
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
Modification Type: | Minor | Last Reviewed: | 10/11/2006 |
---|
Keywords: | kbdtacode kberrmsg kbprb KB213530 |
---|
|