Partager via


MenuItems.AddAt Method

Visio Automation Reference

Creates a new MenuItem object at a specified index in the MenuItems collection. .

Version Information
 Version Added:  Visio 4.0

Syntax

expression.AddAt(lIndex)

expression   A variable that represents a MenuItems object.

Parameters

Name Required/Optional Data Type Description
lIndex Required Long The index at which to add the object.

Return Value
MenuItem

Remarks

If the index is zero (0), the object is added at the beginning of the collection.

The beginning of a MenuItems collection is the topmost menu item. For example, the New Window menu item is the first menu item in the MenuItems collection for the WindowMenu object.

Example

The following macro shows how to add a menu and menu item to the drawing window menu set. This macro uses the AddAt method to add a menu before the Microsoft Office Visio Window menu.

This example assumes that you already have a macro called "MyMacro" in the Microsoft Visual Basic for Applications (VBA) project associated with the active Visio document.

Visual Basic for Applications
  Public Sub AddAt_Example() 
    Dim vsoUI As Visio.UIObject 
    Dim vsoMenuSets As Visio.MenuSets 
    Dim vsoMenuSet As Visio.MenuSet   
    Dim vsoMenus As Visio.Menus 
    Dim vsoMenu As Visio.Menu 
    Dim vsoMenuItems As Visio.MenuItems 
    Dim vsoMenuItem As Visio.MenuItem
'Get a UI object that represents Microsoft Office Visio built-in menus.
Set vsoUI = Visio.Application.BuiltInMenus

'Get the MenuSets collection.
Set vsoMenuSets = vsoUI.MenuSets

'Get the drawing window menu set.
Set vsoMenuSet = vsoMenuSets.ItemAtID(visUIObjSetDrawing) 

'Get the Menus collection.
Set vsoMenus = vsoMenuSet.Menus 

'Add a Demo menu before the Window menu.
Set vsoMenu = vsoMenus.AddAt(7) 
vsoMenu.Caption = "Demo" 

'Get the MenuItems collection.
Set vsoMenuItems = vsoMenu.MenuItems 

'Add a menu item to the new Demo menu.
Set vsoMenuItem = vsoMenuItems.Add 

'Set the properties for the new menu item. 
vsoMenuItem.Caption = "Run &MyMacro" 
vsoMenuItem.AddOnName = "ThisDocument.MyMacro" 
vsoMenuItem.ActionText = "Run MyMacro" 

'Tell Visio to use the new UI when the document is active. 
ThisDocument.SetCustomMenus vsoUI 

End Sub

See Also