Creating a Shortcut Menu

1. Enter the Create_ShortMenu in the Code window of the current VBA project, as shown below:

Sub Create_ShortMenu() Dim sm As Object

Set sm = Application.CommandBars.Add("Information", msoBarPopup) With sm

.Controls.Add(Type:=msoControlButton).Caption = "Operating System" With .Controls("Operating System") .FaceId = 1954 .OnAction = "OpSystem" End With

.Controls.Add(Type:=msoControlButton).Caption = "Total Memory" With .Controls("Total Memory")

.OnAction = "PrintSheet

Print..,

Insert.,,

Delete

Rename

Move or Copy.,,

Select All Sheets

lab Color,,.

View Code

Figure 12-10: A custom option can be added to a built-in shortcut menu (see the Print option that was added by the AddToPlyMenu procedure).

.Faceld = 1977 .OnAction = "TotalMemory" End With

.Controls.Add(Type:=msoControlButton).Caption = "Used Memory" With .Controls("Used Memory") .Faceld = 2081 .OnAction = "UsedMemory" End With

.Controls.Add(Type:=msoControlButton).Caption = "Free Memory" With .Controls("Free Memory") .Faceld = 2153 .OnAction = "FreeMemory" End With End With End Sub

The above procedure creates a custom shortcut menu named Information and adds four commands to it. Notice that each command is assigned an icon. When you select a command from this shortcut menu, one of the procedures shown below in step 2 will run.

2. Enter the following procedures that are called by the Create_Short-Menu procedure:

Sub FreeMemory( )

MsgBox Application.MemoryFree & " bytes", , "Free Memory" End Sub

Sub OpSystem( )

MsgBox Application.OperatingSystem, , "Operating System" End Sub

Sub TotalMemory( )

MsgBox Application.MemoryTotal, , "Total Memory" End Sub

Sub UsedMemory( )

MsgBox Application.MemoryUsed, , "UsedMemory" End Sub

To display the custom shortcut menu named Information on the screen, use the ShowPopup method, as shown in step 3.

3. Enter the following statement in the Immediate window:

CommandBars("Information").ShowPopup 0, 0

The ShowPopup method for the CommandBar object accepts two optional arguments (x, y), that determine the location of the shortcut menu on the screen. In the above example, the Information shortcut menu will appear at the top left-hand corner of the screen.

Suppose that you are designing a custom form and would like to display a shortcut menu when the user right-clicks a command button:

1. Choose Insert | UserForm from the Basic Editor menu.

2. Using the CommandButton control in the Toolbox, place a button anywhere on the empty user form.

3. Switch to the Code window for the form by clicking the View Code button in the Project Explorer window.

4. Enter the following procedure in the UserForm1 (Code) window:

Private Sub CommandButton1_MouseDown(ByVal Button _

As Integer, _ ByVal Shift As Integer, _ ByVal X As Single, _ ByVal Y As Single)

If Button = 2 Then

Call Show_ShortMenu

Else

MsgBox "You must right-click this button." End If End Sub

This procedure calls the Show_ShortMenu procedure when the user right-clicks the command button placed on the form. Visual Basic has two event procedures that are executed in response to clicking a mouse button. When you click a mouse button, Visual Basic executes the MouseDown event procedure. When you release the mouse button, the MouseUp event occurs.

The MouseDown and MouseUp event procedures require the following arguments:

■ The object argument specifies the object. In this example, it's the name of the command button placed on the form.

■ The Button argument is the Integer value that specifies which mouse button was pressed.

Button Argument Value Meaning

1 left mouse button

2 right mouse button

3 middle mouse button

■ The Shift argument determines whether the user was holding the Shift, Ctrl, or Alt keys when the event occurred.

Shift Argument Value Meaning

1 Shift key

2 Ctrl key

3 Shift and Ctrl keys

4 Alt key

5 Alt and Shift keys

6 Alt and Ctrl keys

7 Alt, Shift, and Ctrl keys

5. Enter the code of the Show_ShortMenu procedure in the current project's module:

Sub Show_ShortMenu()

Dim shortMenu As Object

Set shortMenu = Application.ComrrandBars("Infonration") With shortMenu .ShowPopup End With

End Sub

Notice that the ShowPopup method used in this procedure does not include the optional arguments that determine the location of the shortcut menu on the screen. Therefore, the menu appears where the mouse was clicked (Figure 12-11).

6. To delete the shortcut menu named Information, enter and then run the following Delete_ShortMenu procedure in the Code window:

Sub Delete_ShortMenu()

Application.ComrrandBars("Infonration").Delete End Sub

Sub Delete_ShortMenu()

Application.ComrrandBars("Infonration").Delete End Sub

klommandButtonl

e

Operating System

o

Total Memory

Used Memory

TTf

Free Memot y

Figure 12-11: A custom shortcut menu appears when you right-click an object.

Figure 12-11: A custom shortcut menu appears when you right-click an object.

0 0

Post a comment