AddMenuItem Method (ActiveX)

Adds a popup menu item to a popup menu.

Supported platforms: Windows only

Signature

VBA:

RetVal = object.AddMenuItem(Index, Label, Macro)
object

Type: PopupMenu

The object this method applies to.

Index

Access: Input-only

Type: Variant

The index location in the popup menu where the item is to be added. The index must be either an integer or a string.

If an integer is supplied to specify a given location in the menu, the index must be between 0 and N-1, where N is the number of objects in the popup menu. The new item will be added immediately before the specified index location. To add the new menu item to the end of a menu, set the index to be greater than N.

If a string is specified and the indexed item does not exist, then the new menu item is added at the end of the menu.

Label

Access: Input-only

Type: String

The label for the menu item. The label may contain DIESEL string expressions. Labels also identify the accelerator keys (keyboard key sequences) that correspond to the menu item by placing an ampersand (&) in front of the accelerator character.

Macro

Access: Input-only

Type: String

The name of the macro associated with this menu item.

Return Value (RetVal)

Type: PopupMenuItem

The newly created popup menu item.

Remarks

No additional remarks.

Examples

VBA:

Sub Example_AddMenuItem()
    ' This example creates a new menu called TestMenu and inserts a menu item
    ' into it. The menu is then displayed on the menu bar.
    ' To remove the menu after execution of this macro, use the Customize Menu
    ' option from the Tools menu.
    
    Dim currMenuGroup As acadMenuGroup
    Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item(0)
    
    ' Create the new menu
    Dim newMenu As AcadPopupMenu
    Set newMenu = currMenuGroup.Menus.Add("TestMenu")
    
    ' Add a menu item to the new menu
    Dim newMenuItem As AcadPopupMenuItem
    Dim openMacro As String
    ' Assign the macro string the VB equivalent of "ESC ESC _open "
    openMacro = Chr(3) & Chr(3) & Chr(95) & "open" & Chr(32)
    
    Set newMenuItem = newMenu.AddMenuItem(newMenu.count + 1, "Open", openMacro)
    
    ' Display the menu on the menu bar
    newMenu.InsertInMenuBar ThisDrawing.Application.MenuBar.count + 1
    
End Sub

Visual LISP:

(vl-load-com)
(defun c:Example_AddMenuItem()
    ;; This example creates a new menu called TestMenu and inserts a menu item
    ;; into it. The menu is then displayed on the menu bar.
    ;; To remove the menu after execution of this macro, use the Customize Menu
    ;; option from the Tools menu.
    (setq acadObj (vlax-get-acad-object))
    (setq currMenuGroup (vla-Item (vla-get-MenuGroups acadObj) 0))
    
    ;; Create the new menu
    (setq newMenu (vla-Add (vla-get-Menus currMenuGroup) "TestMenu"))
    
    ;; Add a menu item to the new menu
    ;; Assign the macro string the VB equivalent of "ESC ESC _open "
    (setq openMacro (strcat (Chr 3) (Chr 3) (Chr 95) "open" (Chr 32)))
    (setq newMenuItem (vla-AddMenuItem newMenu (1+ (vla-get-Count newMenu)) "Open" openMacro))
    
    ;; Display the menu on the menu bar
    (vla-InsertInMenuBar newMenu (1+ (vla-get-Count (vla-get-MenuBar acadObj))))

    ;; Display the menu bar
    (vla-SetVariable doc "MENUBAR" 1)
)