Search code examples
vbahidemenuitemvisio

How to hide a popup menu item shown for a drawing shape in Microsoft Visio?


I would like to hide a few of the items that are shown in the popup menu when right-clicking on a drawing shape in Visio.

The code I tried. There is no change seen.

 Sub HideVisioMenus()
    Dim uiObj As Visio.UIObject
    Dim menuSetObj As Visio.MenuSet
    Dim menuItemsObj As Visio.menuitems
    Dim i As Integer
    Set uiObj = Visio.Application.BuiltInMenus
    Set menuSetObj = uiObj.MenuSets.ItemAtID(visUIObjSetDrawing)    
    Set menuItemsObj = menuSetObj.Menus(8).menuitems
    'Get the Show ShapeSheet menu item by its CmdNum property.
    For i = 0 To menuItemsObj.Count - 1
        Debug.Print menuItemsObj.Item(i).Caption
        If menuItemsObj(i).CmdNum = visCmdWindowShowShapeSheet Then
            menuItemsObj.Item(i).Visible = False            
            Exit For
        End If
    Next i   
    Visio.Application.SetCustomMenus uiObj    
 End Sub

Solution

  • Which version of Visio are you using? I've been fiddling with the RibbonUI for so long, I forgot about hiding/removing items using CommandBars.

    I honestly couldn't remember if it even works with the ribbon. So I fiddled around and it does work!

    I think that you need this menuset id, though:

    Visio.visUIObjSetCntx_DrawObjSel

    However, walking through the items in that set doesn't reveal the Show ShapeSheet item. So that item is added in some special way by Visio.

    I fiddled with some code and was able to hide everything but Show ShapeSheet and Hyperlinks. No idea how to get rid of those!

    Sub DinkWithRightClickShapeMenu()
    
      '// The following example demonstrates how to retrieve
      '// the currently active user interface for your document
      '// without replacing the application-level custom user
      '// interface, if any.
      
      '// Check if there are document custom menus.
      If ThisDocument.CustomMenus Is Nothing Then
        'Check if there are Visio custom menus.
        If Visio.Application.CustomMenus Is Nothing Then
          'Use the built-in menus.
          Set visUiObj = Visio.Application.BuiltInMenus
        Else
          'Use the Visio custom menus.
          Set visUiObj = Visio.Application.CustomMenus.Clone
        End If
      Else
        'Use the file custom menus
        Set visUiObj = ThisDocument.CustomMenus
      End If
      
      
      Dim menuSetObj As Visio.MenuSet
      Dim menuItemsObj As Visio.MenuItems
      Dim i As Integer, j As Integer
      
      '// This is the menu set for right-clicking a shape:
      Set menuSetObj = visUiObj.MenuSets.ItemAtID(Visio.visUIObjSetCntx_DrawObjSel)
      'Set menuSetObj = visUIObj.MenuSets.ItemAtID(Visio.visUIObjSetCntx_BuiltinMenus)
      
      '// List the menu items in the menu set.
      '// For Each doesn't work:
      Dim mnu As Visio.Menu
      Dim mnuItem As Visio.MenuItem
      For i = 0 To menuSetObj.Menus.Count - 1
      
        Set mnu = menuSetObj.Menus.Item(i)
        Debug.Print "Menu: " & i & ". '" & mnu.Caption & "'"
            
        For j = 0 To mnu.MenuItems.Count - 1
          Set mnuItem = mnu.MenuItems(j)
          Debug.Print j, mnuItem.Caption
          
          '// Hide every menu item:
          mnuItem.Visible = False
          '// This was a test to see if I could change the menu text:
          '//mnuItem.Caption = mnuItem.Caption & " woohoo"
          Debug.Print vbTab & mnuItem.Caption
        Next j
        
      Next i
      
      '// Unfortunately, there are still two items left:
      '// - Show ShapeSheet
      '// - Hyperlinks...
        
      Call Visio.ActiveDocument.SetCustomMenus(visUiObj)
      'ThisDocument.SetCustomMenus uiObj
      'Call Visio.Application.SetCustomMenus(visUiObj)
    
      
      '// Restore the normal menus running this in the
      '// Immediate window:
      'Visio.ActiveDocument.ClearCustomMenus
      
      '// Cleanup:
      Set mnuItem = Nothing
      Set mnu = Nothing
      Set menuSetObj = Nothing
      Set visUiObj = Nothing
      
    End Sub