如何在带有OpenXml SDK 2.0的Word 2007中更改内容控件的内容? [英] How do you change the content of a content control in Word 2007 with OpenXml SDK 2.0?

查看:117
本文介绍了如何在带有OpenXml SDK 2.0的Word 2007中更改内容控件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即将生气这个问题.我敢肯定它是如此的简单,我只是想念它,但是我无法终生发现如何使用C#中的OpenXml SDK v2.0更改Word 2007中内容控件的内容.

About to go mad with this problem. I'm sure it's so simple I'm just missing it, but I cannot for the life of me find out how to change the content of a content control in Word 2007 with the OpenXml SDK v2.0 in C#.

我创建了一个带有纯文本内容控件的Word文档.该控件的标签为名字".在代码中,我想打开Word文档,找到此内容控件,然后在不丢失格式的情况下更改内容.

I have created a Word document with a plain text content control. The tag for this control is "FirstName". In code, I'd like to open up the Word document, find this content control, and change the content without losing the formatting.

我终于开始工作的解决方案包括找到内容控件,在其后插入运行,然后像这样删除内容控件:

The solution I finally got to work involved finding the content control, inserting a run after it, then removing the content control as such:

using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(filePath, true)) {
MainDocumentPart mainDocumentPart = wordProcessingDocument.MainDocumentPart;
SdtRun sdtRun = mainDocumentPart.Document.Descendants<SdtRun>()
 .Where(run => run.SdtProperties.GetFirstChild<Tag>().Val == "FirstName").Single();

if (sdtRun != null) {
 sdtRun.Parent.InsertAfter(new Run(new Text("John")), sdtRun);
 sdtRun.Remove();
}

这确实会更改文本,但我会丢失所有格式.有人知道我该怎么做吗?

This does change the text, but I lose all formatting. Does anyone know how I can do this?

推荐答案

我发现了一种更好的方法,可以使用

I found a better way to do the above using http://wiki.threewill.com/display/enterprise/SharePoint+and+Open+XML#SharePointandOpenXML-UsingWord2007ContentControls as a reference. Your results may vary but I think this will get you off to a good start:

using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filePath, true)) {
    var sdtRuns = mainDocumentPart.Document.Descendants<SdtRun>()
        .Where(run => run.SdtProperties.GetFirstChild<Tag>().Val.Value == contentControlTagValue);

    foreach (SdtRun sdtRun in sdtRuns) {
        sdtRun.Descendants<Text>().First().Text = replacementText;
    }

    wordprocessingDocument.MainDocumentPart.Document.Save();
}

我认为以上内容仅适用于纯文本内容控件.不幸的是,它并没有摆脱最终文档中的内容控制.如果可以,我将其发布.

I think the above will only work for Plain Text content controls. Unfortunately, it doesn't get rid of the content control in the final document. If I get that working I'll post it.

http://msdn.microsoft.com/en-us/library/如果要查找富文本格式的内容控件,cc197932.aspx 也是一个很好的参考.这是关于将行添加到放置在RTF内容控件中的表中的讨论.

http://msdn.microsoft.com/en-us/library/cc197932.aspx is also a good reference if you want to find a rich text content control. This one talks about adding rows to a table that was placed in a rich text content control.

这篇关于如何在带有OpenXml SDK 2.0的Word 2007中更改内容控件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆