gpt4 book ai didi

c# - 检查在 MS Word 中单击了哪个字段

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:54 24 4
gpt4 key购买 nike

我需要在单击字段后显示的 Word 中的右键单击菜单中添加一个命令。那不是问题:

var ContextMenu = this.Application.CommandBars["Fields"];
button = (Office.CommandBarButton)ContextMenu.Controls.Add(1);
button.Click += new Office._CommandBarButtonEvents_ClickEventHandler(button_Click);

现在我需要点击现场用户。我试过这个:

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
}

但它只有在该字段被选中时才会起作用,例如,当用户只是右键单击它而没有选择任何东西或只选择了显示文本的部分字段时,它不会起作用。

最佳答案

您可以通过检查 currentSelection.Range.Paragrahs[1].Fields 显着减少迭代的字段数。

关于c# - 检查在 MS Word 中单击了哪个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11243752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com