0

I have a Word VBA Template that accomodates Old and New Versions. Posted below accomodates the old style. The new one is an Add In that works fine as well. The problem is simply that the customized menus work when right clicking outside an inserted table but not when right clicking inside it. PS: I actually don't think posting this code is really necessary because It seems that customized menus don't work inside a table.

Public Sub BuildCustomMenus2()
    On Error Resume Next
    With Application.CommandBars("Menu Bar")
        .Controls("rate1").Delete
    End With
    
    Dim aVal As Single
    aVal = ToDoubleFromString(Application.Version)
    

    If aVal < 12 Or Trim$(GetSetting("rate1", "Narrative Software", "NoWordRibbon")) = "Yes" Then
        If ThisDocument.Application.Windows.count > 0 Then SetN1Defaults
        
        Dim vCtrlCount As Integer
        Dim ctrlControl As CommandBarControl
    
        'delete any previously created custom toolbars and rebuild
        On Error Resume Next
        With Application.CommandBars("Menu Bar")
            .Controls("rate1").Delete
        End With
    
        vCtrlCount = CommandBars("Menu Bar").Controls.count
        vCtrlCount = vCtrlCount + 1
    
        With CommandBars("Menu Bar").Controls
            .Add(Type:=msoControlPopup, BEFORE:=vCtrlCount).Caption = "&rate1"
        End With
    
        'add more macros to options here...and create
        'sub...add to select case statement
        
        '1. Add the buttons
        '2. title caption
        '3. Display only the caption text
        '4. call procedure to pass button call
        Set ctrlControl = CommandBars("Menu Bar").Controls("rate1").Controls.Add(msoControlButton)
        ctrlControl.Caption = "Navigate"
        ctrlControl.Style = msoButtonIconAndCaption
        ctrlControl.OnAction = "RunDocMap"
        ctrlControl.FaceId = 1714
        
        
        Set ctrlControl = CommandBars("Menu Bar").Controls("rate1").Controls.Add(msoControlButton)
        ctrlControl.Caption = "Mark Active Edit"
        ctrlControl.Style = msoButtonIconAndCaption
        ctrlControl.OnAction = "MarkActiveEdit"
        ctrlControl.FaceId = 279
        
        Set ctrlControl = CommandBars("Menu Bar").Controls("rate1").Controls.Add(msoControlButton)
        ctrlControl.Caption = "Go to Active Edit"
        ctrlControl.Style = msoButtonIconAndCaption
        ctrlControl.OnAction = "GoToActiveEdit"
        ctrlControl.FaceId = 39    

End Sub

  • Remove **all** `On Error Resume Next` and see what errors you get. These lines hide **all** error messages until `End Sub` but the errors still occur. If you cannot see the errors you cannot fix them, if you don't fix them your code cannot work. Remove them! • You might benefit from reading [VBA Error Handling – A Complete Guide](https://excelmacromastery.com/vba-error-handling). – Pᴇʜ Jul 22 '21 at 15:10
  • The code is good. I get your point. This isn't about error handling. My apologies for posting this code. Anyways, I still followed your suggestion and no error occurs. My question is simple. Try creating a simple Code that adds a sub menu in right click pop up menu. When you see it's good, Try inserting a table in your Word Doc, and right click inside it, see that all customized right click menu won't show up. Error is not an issue here. I have read about a cell issue in Excel which is similar to this but it hasn't been answered as well. – D.P. OL' RACE Jul 22 '21 at 16:05
  • You do realize that the above code is for Word 2003 or earlier, don't you? – Timothy Rylatt Jul 22 '21 at 18:08
  • CommandBars were deprecated 14 years ago, you're lucky you get any result at all. Here's Greg Maxeys' page on the subject, there may be some help there: https://gregmaxey.com/word_tip_pages/customize_shortcut_menu.html Consider modifying RibbonUI XML, which is the current technology for custom context menus. – John Korchok Jul 22 '21 at 18:08
  • @JohnKorchok - the code is only valid if the Word version number is less than 12, so 2003 or earlier. It won't work at all for ribbon versions. – Timothy Rylatt Jul 22 '21 at 18:16
  • Again, My Apologies for posting the code. Note that We have that two set of codes to accomodate old and new versions of Word--I have added the new style but I did not bother to put all Add In codes here. It really doesn't matter which one is which. My question is just a general question: Why is it that customized right click menus not working inside a table. Both work when a picture is added outside a table and a user right clicks and the pop up menu shows up i.e. Update Selected Pix etc... @TimothyRylatt JohnKorchok – D.P. OL' RACE Jul 23 '21 at 14:48
  • A right-click menu is called a "context menu". The context, i.e. where the cursor is located/what is selected, determines which of them is displayed. So to add customizations you need to target the correct context menu. – Timothy Rylatt Jul 23 '21 at 15:17
  • Thanks @TimothyRylatt. That's enlightening. Actually I just also tried opening the Add In of our software but one of its component where I'm pretty sure is where the XML are, is a compiled file or something and It can't be opened; hence, I can't really show it for scrutiny. But your answer is making sense to me now. – D.P. OL' RACE Jul 26 '21 at 06:48
  • VBA code is not XML. To see the code you'll need to open the add-in in Word. If you're smart you'll work out how to remove the password that protects the code, though you'll need Word 2010 to do it. – Timothy Rylatt Jul 26 '21 at 10:03
  • @TimothyRylatt Lol. Of course, I know VBA is not XML. the Ribbon for this Context Menu as you've said it is in XML. This is not something made out of Upwork or somewhere. The file that contains it, is called from Word using VBA. I know what you are saying we could open the Add In. But it's not the Add in really that I can't open it's a compiled file that is an octet-stream, charset=binary inside the Add In. It has a .bin extension. The Add In is not made ordinarily from Word or Excel as we VBA programmers do. If converted to zip: There are 2 binary proprietary(?) files there and lots of XMLs. – D.P. OL' RACE Jul 26 '21 at 12:21
  • The ***only*** way to access the VBA code is to open the add-in in Word. What you are describing is a macro enabled Word file. The .bin contains the VBA and is identical to every macro enabled file "made ordinarily from Word or Excel as we VBA programmers do" – Timothy Rylatt Jul 26 '21 at 13:07
  • Public Sub CallN1Function(ctlRibbon As IRibbonControl) Dim x As Variant x = Replace(ctlRibbon.Tag, "xxxxxx!", "") Application.Run x End Sub – D.P. OL' RACE Jul 26 '21 at 14:27
  • The code is the only one inside the Add-In Word as shown in the VBE. Actually, I just opened that as you've suggested using this link: https://stackoverflow.com/questions/23821295/word-vba-getting-project-is-unviewable-error/24357424. Also that .bin file? I tried to add it as an Add In like we VBA Programmers do, but it's not being recognized as an Add In. It's part of that "Application.run" in the code. I can open the XML file inside the Add In for Ribbon Customization, though--which we have alter sometimes. If that helps. If you can suggest on how to open the .bin file. I'd gladly accept it. – D.P. OL' RACE Jul 26 '21 at 14:32
  • @TimothyRylatt Please see the Add In code in this thread.I also forgot that these Word and Excel VBA programs are part of an Application. If that also helps. But I know you are correct because there are two .bin file that can't be opened. But first, granting we can open both, we need to open first the Context Menus(RIght Click Menu) XML to know which functions were called. But even then. It's not almost relevant to the question above I posted because they're all fine. They just won't work when we right click inside a table in word. In short, the Context Menu Target for tables. – D.P. OL' RACE Jul 26 '21 at 15:50
  • Assuming that what you describe in your question is indeed "a Word VBA Template" all the ribbon xml, including any context menus, is contained in a single file in the CustomUI folder. If you want any further information you need to post a new question with a more detailed explanation of any issues and the files you are working with, perhaps including screenshots of the file structure to aid understanding. – Timothy Rylatt Jul 26 '21 at 16:41

1 Answers1

0

Prior to Microsoft Office 2010, the only way to customize context (right-click) menus in the Microsoft Office Fluent Ribbon user interface (UI) was by using CommandBars solutions. In Office 2010, you can customize built-in context menus just as you can the other components of the Ribbon UI. This XML-based context menu extensibility model is based on the familiar Ribbon extensibility model. This means that you can use the same XML markup and callbacks that you currently use to customize the Ribbon UI.

Command bars were deprecated with MS Office 2010. Use the Fluent UI (aka Ribbon UI) for customizing context menus in Office 2010 and later. See Customizing Context Menus in Office 2010 for more information. For example:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
   <contextMenus>
      <contextMenu idMso="ContextMenuText">
         <button idMso="FontDialog" visible="false" />
         <toggleButton id="MyToggle" label="My Toggle Button" />
         <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
         <menuSeparator id="MySeparator" />
         <menu id="MySubMenu" label="My Submenu" >
            <button id="MyButton2" label="Button on submenu" />
         </menu>
         <gallery id="galleryOne" label="My Gallery">
            <item id="item1" imageMso="HappyFace" />
            <item id="item2" imageMso="HappyFace" />
            <item id="item3" imageMso="HappyFace" />
            <item id="item4" imageMso="HappyFace" />
         </gallery>
         <dynamicMenu id="MyDynamicMenu" label= "My Dynamic Menu" getContent="GetMyContent" />
      </contextMenu>
   </contextMenus>
</customUI>

If the table is an embedded OLE object (Excel worksheet) you need to customize the context menu in Excel in that case.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks! Unfortunately, I just found that our Add In contains a compiled file where the XML for these are. So this leaves us to the option to mimic all the functionalities we have or try to decompile it and make the necessary changes. Thanks. At least we know where we will start. – D.P. OL' RACE Jul 26 '21 at 06:54