如何在基于C#的Word加载项中从Word文档中获取选定的文本?
问题描述:
我正在开发MS Word加载项.为此,我想知道如何获取Word文档中当前选择的文本,然后对其执行特定于应用程序的操作.
I am developing a MS Word Add-In. For which, I would like to know how to get the text that is currently selected in the word document and then perform my application specific action on it.
谢谢您的帮助.
答
在Visual Studio的Word加载项项目中,对触发事件使用以下代码来获取所选文本:
In your Word add-in project in Visual studio use the following code on your trigger event to get the selected text:
string selectText = string.Empty;
Word.Selection wordSelection = this.Application.Selection;
if (wordSelection != null && wordSelection.Range != null)
{
selectText = wordSelection.Text;
}
注意:以上代码尚未经过测试.
note: the above code hasn't been tested.