使用Windows窗体将文本添加到Word文档中的richtextcontentcontrol

问题描述:

大家好,

我正在构建一个项目,我想在其中添加文本到富文本内容控件中单击随机窗体上的按钮。

I'm building a project in which I want to add text to a richtextcontentcontrol in word by clicking a button on a random windows form.

我想将文本"传递"到word文档,该文档将添加到Visual Studio的项目中。

I want to 'pass' de text to the word document, which is added in the project in Visual Studio.

在提供的链接中,包含文件。

In the provided link, the documents are included.

https://www.dropbox.com/sh/kwhevbxyvn0ic77/AACXF1qW12vVlGuCXMDFqj-la?dl=0

https://www.dropbox.com/sh/kwhevbxyvn0ic77/AACXF1qW12vVlGuCXMDFqj-la?dl=0

提前致谢!

您好,

我有下载了你的项目,但我仍然想知道你如何在项目中使用该表格?

I have downloaded your project but I'm still wondering how do you use the form in the project?

我认为这个过程是获取文本 - >获取控制对象 - >设置文本到控件对象,对吗? 哪个部分代码让你感到困惑?

I think the process is Getting the text->Getting the control object->Setting the text to the control object, right? Which part code puzzles you?

我认为关键点可能是如何访问richtextcontentcontrol。如果是这样,我建议你为控件设置一个标签,例如"Test"。然后你可以使用

SelectContentControlByTag
返回对象然后将文本设置为对象的方法。

I think the key point maybe how to access the richtextcontentcontrol. If so, I would suggest you set a tag for the control, such as "Test". And then you could use SelectContentControlByTag method to return the object and then set text to the object.

这是一个简单的代码。

//not sure how would you get the document object, you could get it by your way
	    Word.Document wdDoc = Globals.ThisDocument.Application.ActiveDocument;
            Word.ContentControl contentControl = wdDoc.SelectContentControlsByTag("Test")[1];
            contentControl.Range.Text = "This is added text string";

最好的问候,

Terry