VSTO 中的插件 - 如何使用带按钮的功能区从 Word 文档中获取文本

问题描述:

如何使用 word 插件中的按钮对 word 文档中的文本进行处理?我不知道点击按钮后如何从文档中获取文本,因为在 Ribbon 类中无法访问应用程序.

How can I do something with text in my word document using button in word add-in? I don't know, how to get text from document after click on the button, because Application is not accessible in Ribbon class.

以下代码应采用在选择中找到的文本:

The following code should take the text found in the selection:

string textFromDoc;
textFromDoc = Globals.ThisAddIn.Application.Selection.Text;

您还可以通过以下方式指定Range:

You can also specify the Range by:

textFromDoc = Globals.ThisAddIn.Application.ActiveDocument.Range(0, 20).Text;

这将采用活动文档中的前 20 个字母.最重要的是,您可以使用以下方式访问应用程序:

This will take the first 20 letters written in the active document. Bottom line, you can access Application by using:

Globals.ThisAddIn.Application