Reputation: 409
Simply put, playing with MS Word's contextual menus via VBA is creating messy results. I'm not sure what I'm doing wrong since this should be simple.
Here's the situation:
I created a macro that will add right-click options to check Google and a few other sites like Wikipedia and Thesaurus.com, but sometimes the command will be added more than once, sometimes not at all, and the menu will never be reset. Code with Google below (but the code is the same for adding each site):
Dim DoesGoogleOptionExist As CommandBarControl
Dim ContextControl As CommandBarControl
Application.CommandBars("Text").Reset
Set DoesGoogleOptionExist = Application.CommandBars(CommandBar(CommandBarCounter)).FindControl(Tag:="Google")
If (DoesGoogleOptionExist Is Nothing) Then
Set ContextControl = Application.CommandBars(CommandBar(CommandBarCounter)).Controls.Add(Type:=msoControlButton, Temporary:=True, Before:=1)
With ContextControl
.Caption = "Check &Google.com"
.OnAction = "OpenGoogle"
.Tag = "Google"
.FaceId = 86
End With
Weird thing here: even though I check to see if the option exists whenever I run the macro, sometimes it will be created again anyway. Sometimes it won't even be created once. So I wanted to start this whole thing from scratch. I reset the menu at the start of the macro. The reset commadn won't reset the menus: the options still exist!
So I made a quick test: reset the menu and comment out the rest of the code so nothing gets added. So nothing should show up. But when I right-click, everything is still there. I list the options in VBA, and sure enough, they're all still on the list:
Dim CommandBarCounter As Integer
For CommandBarCounter = 1 To Application.CommandBars("Text").Controls.Count
Debug.Print Application.CommandBars("Grammar").Controls(CommandBarCounter).Caption
Next CommandBarCounter
Application.CommandBars("Text").Reset
How is Reset not doing its one job properly? Why is it sometimes some options are duplicated, other times they're completely missing? I'd like to (1) reset the menu, then (2) add each option only once.
Thanks, everyone.
Important edit: New details. I realized I'm working in two different templates, so sometimes the menu option will be added to one template or another. Not sure how that happened, but I'm now implicitly going to state which template. I'll leave this question open and unanswered because the problem isn't quite resolved, so feel free to add comments or solutions. But this itself might be the solution why things aren't resetting, are duplicating, or are missing.
Now there's a new problem: while the options are being added to most menus (grammar, spelling, etc.) not all are being added to the text menu. It's adding two fewer than the number I add. So if I add 4, it'll really add 2. If I add 3, it'll add 1: always the first one, then the last one (if there are more than 3).
So, if I'm adding options to go to dictionary, thesaurus, Wikipedia, and Google, it'll add dictionary and Google (but not the middle two). If I add options to dictionary, thesaurus, and Wikipedia, it'll add dictionary (but not the last two). I can rearrange the order of the menu options added, but it will always add the first, then the last, never the middle ones.
Just the text menu. All other menus seem to be working as they should.
I've verified this (i.e. made sure they were added but just invisible) in VBA.
No explanation yet. Anyone come across this before?
Upvotes: 0
Views: 425
Reputation: 25693
Background
The Office CommandBars
functionality is no longer the official way to customize menus. Since Office 2010, when the Fluent UI (RibbonX) was extended to include context menus, that approach has been deprecated.
The object models still work, if for no other reason than backwards compatibility, and there's no published limit for when things might stop working. Considering the fact that WordBasic still works where the underlying technology hasn't changed, I don't expect the functionality will be forced out, but there's no guarantee that the effort put into developing a solution with deprecated technology will be worth while in terms of longevity...
That said, the problem described is a well-known one from the decade of version 97 through 2007, although I don't find a duplicate describing the issue here on Stack Overflow. The problem is Word's Customization Context.
Customization context
Word can store things like keyboard shortcuts and CommandBar customizations in various places (files). By default, that will often be the Normal.dotm template. It could, however, also be the attached template (if it's not Normal.dotm) of the active document or the document containing the macro, or the active document. What's more, Word might choose to add into one customization context and delete from another, resulting in duplicates, things not deleted, and so on.
The only way to control this is to specify the customization context in the code before a procedure works with any CommandBar
or Keybindings
objects.
CustomizationContext
is a property of the Application
object, so some examples are:
save changes to menus in the document which is active when the macro runs
Application.CustomizationContext = ActiveDocument
'save changes to menus in the template attached to the active document
Application.CustomizationContext = ActiveDocument.AttachedTemplate
'save changes to the Normal.dotm template, meaning they will be available
'in all open documents, UNLESS menus in an active document override
Application.CustomizationContext = NormalTemplate
As noted in the comment to the last line of sample code, menus/keybindings can be overridden by what's specified in other "contexts". The rule of thumb is, the more general will be overridden by the more specific. So the settings in a document will override those of the attached template. In turn, those in an attached template will override Normal.dotm or another template loaded as an add-in.
Upvotes: 3