问题
I have the following code to add a menu item on the Word 2013 right click menu:
Sub CreateMenuItem()
Dim MenuButton As CommandBarButton
With CommandBars("Text")
Set MenuButton = .Controls.Add(msoControlButton)
With MenuButton
.Caption = ChrW(&H2714) '"Correct"
.Style = msoButtonCaption
.OnAction = "InsertMark"
End With
End With
End Sub
The issue is that the new menu item is not displayed when I right click on a Text box or a table.
How do I remove a menu item that is not used by me, for example "Translate"
Please Assist Thinus
回答1:
Word provides a completely different CommandBar
object for each context-sensitive menu, rather than dynamically changing the contents of a single menu. That means you need to make the change for each context in which your command should appear.
You can generate a list of CommandBars by looping the collection and writing the various properties out (for example, in a new document). Then you have to inspect that list, pick out the likely candidates, run your code then see if you guessed correctly.
You want to be very careful where these changes are SAVED in Word. If you don't specify it specifically, Word could save the change in one place and delete it (assuming you "undo" your changes) in a different one. This can really muck up the Word configuration and make users extremely unhappy. So always use Application.CustomizationContext
at the beginning of code that changes the CommandBars
. The CustomizationContext
can be any Document
or Template
object.
NOTE: The CommandBars
object is actually deprecated as of Word 2010. From Word 2013 onwards the context menus should be customized using Ribbon XML. Just saying... It's possible that in a future version your code will no longer work.
来源:https://stackoverflow.com/questions/36445064/modify-context-menus