XL: Caption Property of Menu Contains Ampersands

ID: Q113490


The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0


SYMPTOM

In Microsoft Excel versions 5.x and 7.0 for Windows, the Caption property of a Menu or a MenuItem will contain an ampersand character (&) if the Menu or MenuItem has a quick key assigned to it. If an ampersand actually appears in the name of the Menu or MenuItem, the Caption property for that item will contain two ampersands right next to each other.

When you read the Caption property in a Visual Basic macro (for example, if you display this property in a message box), this ampersand will appear as part of the text for the caption.


CAUSE

This behavior occurs because, although ampersands are used to assign quick keys to a Menu or MenuItem, they are interpreted as part of the caption text.


MORE INFORMATION

When you create the Menu or MenuItem, either in a macro or by using the Menu Editor, an ampersand is used to indicate that the next character is the designated quick key.

For example, if you want to add the command "Begin Process" to a menu, and you want the P in "Process" to be a quick key, you would type Begin &Process in the Caption field in the Menu Editor. The "Begin Process" menu item would then appear with an underline under the letter P.

If you want a Menu or MenuItem to actually contain an ampersand, you must enter two ampersands directly adjacent to each other.

For example, to create a Menu called "Shipping & Receiving", you would enter Shipping && Receiving in the Caption field. If you also wanted to assign a quick key to the S in "Shipping", the Caption field would contain "&Shipping && Receiving".

Visual Basic Code Example

Microsoft provides examples of Visual Basic procedures 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 Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."

Example 1--Results of reading the Caption property of a Menu or MenuItem that has a quick key assigned to it

To run this first example, position the cursor in the line which reads "Sub GetCaptions1()" and either press the F5 key or choose Start from the Run menu.

'----------------------------------------------------------------------
Option Explicit

Sub GetCaptions1()

   'Dimension variables.
   Dim Alpha As String

   'Put the Caption of the third menu item of the first menu of the
   'first menu bar into the variable Alpha.
   Alpha = MenuBars(xlWorksheet).Menus(1).MenuItems(3).Caption

   'Show the Caption. It should be "&Close".
   MsgBox Alpha

   'Put the Caption of the first menu of the first menu bar into the
   'variable Alpha.
   Alpha = MenuBars(xlWorksheet).Menus(1).Caption

   'Show the Caption. It should be "&File".
   MsgBox Alpha
End Sub
'---------------------------------------------------------------------- 

Example 2--The results of reading the Caption property of a Menu or MenuItem that actually contains an ampersand

To run the second example, position the cursor in the line which reads "Sub GetCaptions2()" and either press the F5 key or choose Start from the Run menu.

'----------------------------------------------------------------------
Option Explicit

Sub GetCaptions2()

   'Dimension variables.
   Dim Bravo As String

   'Add a menu item "Shipping & Receiving" to the first menu bar.
   'Enter the following two lines as a single line.
   MenuBars(xlWorksheet).Menus.Add Caption:="&Shipping && Receiving", _
      Before:="File"

   'Put the Caption of the first menu of the first menu bar into the
   'variable Bravo.
   Bravo = MenuBars(xlWorksheet).Menus(1).Caption

   'Show the Caption. It should be "&Shipping && Receiving".
   MsgBox Bravo
End Sub
'---------------------------------------------------------------------- 

Additional query words: 5.00c

Keywords : kbprg
Version : WINDOWS:5.0,5.0c,7.0
Platform : WINDOWS
Issue type :


Last Reviewed: October 25, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.