none
Word 2013 Menu Delete RRS feed

  • Question

  • In the process of converting VBA macros from older Word versions to 2013. Eventually plan to move to ribbon controls, but menus will have to suffice for the time being. 

    Running into issue when adding and deleting menus (I add them into the Edit and Insert menus, but they appear on the ribbon as Add-Ins and are shown as Menu Commands). 

    On my system, adding and deleting menus works great, no issues. But when shipped to client sites, the delete commands are ignored. 

    For example, while viewing macros, in the immediate pane, on my system: 

    ?Application.CommandBars("Menu Bar").Controls("&Edit").Controls("Edit &Object").Caption

    Application.CommandBars("Menu Bar").Controls("&Edit").Controls("Edit &Object").Delete
    ?Application.CommandBars("Menu Bar").Controls("&Edit").Controls("Edit &Object").Caption

    On my system: 

    1) shows the caption, 2) deletes the menu, 3) yields an error as the menu item has been deleted. 

    On a customers system: 

    #2 does not delete the menu item. Both #1 and #3 commands show the menu item caption - it has not been deleted. 

    I have to assume that there is some difference in the ?Options? settings between my system and the clients settings, but darned if I can find it. 

    Any ideas; suggestions would be most welcomed. 

    TIA

    Monday, July 10, 2017 5:39 PM

All replies

  • Hi NeilSutter,

    you had mentioned that the code mentioned above is working on your system but it is not working on the client system.

    I guess you are using the older version of the MS Word so it supports the command bars.

    but your client is using MS Word 2013 , which is not supports the command bars.

    so your code is not working on that machine.

    after Office 2007, Command bars are replaced with ribbons.

    if you are also using the Office 2013 for development then I suggest you to post your sample code , I will try to make a test on my side.

    currently, I try to use the code below to add controls but it is not adding the controls.

    Sub AddMenuItem()
        Dim newMenu As CommandBarControl
        Dim toolsMenu As CommandBar
        Set toolsMenu = Application.CommandBars("View")
    
        Set newMenu = _
                toolsMenu.Controls.Add(msoControlButton, , , , True)
    
        newMenu.Caption = "New &Menu Item"
    End Sub

    other code example:

    Sub demo3()
    Dim newItem As Object
    Set newItem = CommandBars("View").Controls.Add(Type:=msoControlButton)
    With newItem
        .BeginGroup = True
        .Caption = "Make Report"
        .FaceId = 0
        .OnAction = "qtrReport"
    End With
    
    End Sub
    
     

    in the Office 2013, I suggest you to use 'Custom UI Editor' to add and remove control to ribbon.

    below links may help you.

    Office Custom UI Editor available

    Below link is for Excel but same link apply for all Office Applications.

    How to: Use VBA to Add a Custom Command to the Ribbon

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 11, 2017 1:24 AM
    Moderator
  •             

    Hi Deepak, 

    Thanks for your reply. Both client and development systems are running Word 2013. As mentioned in the original post, I have not moved to the use of the ribbon model, but am staying with "menus" for the short term. I agree that Word 2013 does not directly support "menus" but if code such as: 

        Set databaseItem = CommandBars("Menu Bar").Controls("Edit").Controls.Add(Type:=msoControlButton)
        With databaseItem
            .Caption = "Edit History &Part"
            .OnAction = "EditHistoryPart"
        End With

    is used, then an Add-Ins ribbon control is added with a Menu Commands region in which the Edit History Part menu appears. 

    There are occasions, however, where that menu option needs to be deleted and readded. The following code is used: 

    Sub DeleteOurCustomMenus()

        Dim mItemCount As Integer
        Dim sItemCount As Integer
        Dim mItem As Integer
        Dim sItem As Integer
        Dim mMenuItem As String
        Dim sMenuItem As String

        ShowAllMenuItems

        mItemCount = CommandBars("Menu Bar").Controls.Count
        For mItem = 1 To mItemCount
            sItemCount = CommandBars("Menu Bar").Controls(mItem).Controls.Count
            mMenuItem = CommandBars("Menu Bar").Controls(mItem).Caption
            sItem = 1
            Do While sItem <= sItemCount
                sMenuItem = CommandBars("Menu Bar").Controls(mItem).Controls(sItem).Caption
                If _
                    sMenuItem = "Edit History &Part" Or _
                    sMenuItem = "Edit Histor&y Part" Or _
                    sMenuItem = "Edit History &Work Item" Or _
                    sMenuItem = "Edit &Base History" Or _
                    sMenuItem = "&Insert History Part" Or _
                    sMenuItem = "Insert Histor&y Part" Or _
                    sMenuItem = "Insert &History Work Item" Or _
                    sMenuItem = "Insert History Wor&k Item" Then
                        CommandBars("Menu Bar").Controls(mItem).Controls(sItem).Delete
                        sItemCount = CommandBars("Menu Bar").Controls(mItem).Controls.Count
                Else
                    sItem = sItem + 1
                End If
            Loop
        Next

    End Sub

    I have also tried the much more direct: 

    CommandBars("Menu Bar").Controls("&Edit").Controls("Edit History &Part").Delete
    CommandBars("Menu Bar").Controls("&Edit").Controls("Edit Histor&y Part").Delete
    etc.

    The reason for the more complex loop above was due to frustation that the direct name/text method was not working. But that does not work either. 

    The bottom line is that on my development system, deleting custom menus works, using either indexes (in the loop) or names. On clients systems, neither works. No error occurs, but the menu items are not deleted. 

    On my clients' computers, I can issue (in macro form or the immediate pane) the following command multiple times: 

    CommandBars("Menu Bar").Controls("&Edit").Controls("Edit History &Part").Delete
    CommandBars("Menu Bar").Controls("&Edit").Controls("Edit History &Part").Delete
    CommandBars("Menu Bar").Controls("&Edit").Controls("Edit History &Part").Delete

    No error occurs, but the menu is not deleted. 

    On my development system, running the above 3 lines deletes and menu item, then issues 2 errors for the 2nd and 3rd delete commands. 

    If an error occurred or an exception thrown, then I'd have an idea of where to look. However, no error occurs, and the delete operation is ignored. 

    What I'm trying to determine is what can differ between their setup and my development setup. I've run through the options on several clients' systems and they look identical to my setup, including trust center, etc. Very frustrating....

    Tuesday, July 11, 2017 4:44 PM
  • Hi NeilSutter,

    did you try to check the exact version of Office 2013 of your development machine and your clients machine?

    it is possible that both are different and because of this different version you are getting different output because something get change during this version updates.

    as I suggested before , you should try to move to the Custom UI Editor to add and remove controls to the ribbon because it's supports this function very well and you will get same output when you try to run that same code on multiple machines.

    with your old approach , it is possible that your code can fail on the other machines.

    as I said before , when I try to test the code on my side I am even not able to add controls.

    so it is better to leave the old approach.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 12, 2017 2:06 AM
    Moderator
  • Hi Deepak, 

    Thanks again for the reply. 

    Is there a programmable way to access the Custom UI Editor in Word 2013? If so, I'll look into that. I cannot use a non-programmable approach due to the number of users and computers involved. 

    TIA

    Neil

    Wednesday, July 12, 2017 12:32 PM
  • Hi NeilSutter,

    I already provided you the link for that in my previous reply.

    how you can use custom UI Editor to generate the XML Ribbon and how you can make call back from VBA.

    below is the same link I post again. note that it can apply to the Word.

    How to: Use VBA to Add a Custom Command to the Ribbon

    you need to open the document in Custom UI Editor and add the XML Ribbon to it.

    then save it and close it and do further code in VBA editor for creating callbacks.

    further to get more control and features for ribbon then you can try to use VSTO Ribbons.

    in that you can design the ribbon by 2 ways and you can use object oriented languages to manipulate it.

    Reference:

    Ribbon Overview

    Walkthrough: Creating a Custom Tab by Using Ribbon XML

    Walkthrough: Creating a Custom Tab by Using the Ribbon Designer

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 13, 2017 2:42 AM
    Moderator
  • Hi Deepak, 

    Thanks yet again. Much appreciate the support and links. I'll review the material in the links. May yet need to move to a "ribbon" solution rather than the current menu solution. Even then, the issue is getting rid of the current custom menus, which is what I've been trying to solve. A manual fix will not work as there are over 3,000 users at multiple sites world-wide. So any solution to remove existing menus will need to be done in VBA, which, of course, is what I cannot get working. 

    Anyway, let me review the materials you referenced and see what I might come up with. 

    Thanks again. 

    Neil

    Thursday, July 13, 2017 3:00 PM
  • Hi NeilSutter,

    you had mentioned that on your development machine the code is working.

    so I had suggested you to check for the version but you had mentioned that both development and client machine are using Word 2013.

    but there is a specific version in Word 2013.

    this version get changed every time when you install any new updates.

    which looks like below.

    so i was suggesting you to check this version on both machine.

    it is possible that your development machine is still using the older version so it can able to run the code.

    but your client has updated the office so on that machine it is not working.

    so just inform them to install the same version of Office 2013 that you are using and remove the controls by running your code.

    then in future , you can develop a code that work with ribbon.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 14, 2017 2:59 AM
    Moderator
  • Hi TIA,

    Has your issue been resolved? I made a test with your code under Office 2013, but I failed to reproduce your issue.

    Could you make a test with below code:

    Sub AddMenu()
    Set databaseItem = CommandBars("Menu Bar").Controls("Edit").Controls.Add(Type:=msoControlButton)
        With databaseItem
            .Caption = "Edit History &Part"
            .OnAction = "EditHistoryPart"
        End With
    End Sub
    Sub RemoveMenu()
        Dim c As CommandBarControl
        For Each c In CommandBars("Menu Bar").Controls("Edit").Controls
            c.Delete
        Next c
    End Sub

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 20, 2017 7:40 AM