如何在基于C#的Word加载项中从Word文档中获取选定的文本? [英] How to get selected text from Word Document in C# based Word Add-In?
本文介绍了如何在基于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.
这篇关于如何在基于C#的Word加载项中从Word文档中获取选定的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文