0

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
Community
  • 1
  • 1
codebug
  • 197
  • 1
  • 3
  • 15

2 Answers2

2

Maybe what you are looking for is actually disabling Developer Mode? (it's in the settings). Unchecking that will hide the "ShapeSheet" command from the context menu. Please note that developer mode is already disabled by default. You can also turn it off programmatically like this:

Application.Settings.DeveloperMode = False

FYI: There are other methods as well, like disabling by registry settings (administration policies). I have a small note on this here: https://unmanagedvisio.com/disabling-visio-built-in-commands/

Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • Yes, you are right. I disabled the Developer mode and the "Show Sheet" vanished. Thank you for this pointer. – codebug Jul 15 '20 at 11:46
  • Now I am struggling to remove the "H&yperlink... " menu item. – codebug Jul 15 '20 at 11:47
  • Also, thank you for the link to look at other methods. – codebug Jul 15 '20 at 11:54
  • 1
    It's basically a bad idea to "remove" stuff like that. You'll need to pay attention to keyboard shortcuts which are harder to remove, etc. You could consider using "startFromScratch" (https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.tools.ribbon.officeribbon.startfromscratch) or just embeding Visio into your own application as a control if you want to limit user actions. – Nikolay Jul 15 '20 at 14:04
  • Thank you for the thoughts Nikolay, I am considering what you pointed. – codebug Jul 23 '20 at 07:38
1

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
Visio Guy
  • 211
  • 1
  • 4
  • Thanks for responding "Visio Guy". I Will check your code and get back. I am using Visio Plan 2. Also, soon after this, I may be getting into the ribbon hiding as well, expecting a rough ride ahead and may need your help! :) – codebug Jul 10 '20 at 06:09
  • Your code works perfectly hiding everything except those 2 menu items. Any pointers on a way to hide them as well? I am still trying to figure out how are those two getting inserted. – codebug Jul 10 '20 at 11:56
  • I'll take a guess that those two items are added conditionally by software internal to Visio, as they are both items that depend upon some other factor, i.e. there being a hyperlink on the shape, or developer mode being enabled. – Paul Herber Jul 10 '20 at 19:51
  • Yes, Paul Herber, Thanks for responding. Looks like that to me, some tries are still pending to test for me to remove those. – codebug Jul 13 '20 at 06:04