I need to add a command to right click menu in Word shown after clicking field.
That wasn’t a problem:
var ContextMenu = this.Application.CommandBars["Fields"];
button = (Office.CommandBarButton)ContextMenu.Controls.Add(1);
button.Click += new Office._CommandBarButtonEvents_ClickEventHandler(button_Click);
Now I need to get the field user clicked. I tried this:
void button_Click(Office.CommandBarButton Ctrl, ref bool cancel)
{
var currentSelection = Globals.ThisAddIn.Application.ActiveWindow.Selection;
if (currentSelection.Fields.Count > 0)
var field = currentSelection.Fields[1]
//Do some stuff with the field
}
But it will only work if the field is selected, it won’t work for example when user just right clicked it without selecting anything or selected only part of fields displayed text.
I came up with this solution but still looking for something that won’t have to iterate throught all fields in document: