有没有办法从Word文档的内容控件中获取内容?

时间:2020-08-17 12:52:19

标签: c# ms-word office-interop word-contentcontrol

通过利用

Application app = new Application();

Document doc = app.Documents.Open(path, ReadOnly: false, Visible: false);

var test = doc.SelectContentControlsByTag("CK");

我得到的每个内容控件都带有标签“ CK”,在我看来,它只是一个。 如何使内容过测试?

1 个答案:

答案 0 :(得分:0)

使用以下方法使它起作用:

 using (WordprocessingDocument doc =
                    WordprocessingDocument.Open(path, false))
            {

                List<SdtBlock> sdtSubTable = doc.MainDocumentPart.Document.Body.Descendants<SdtBlock>().Where
                 (r => r.SdtProperties.GetFirstChild<Tag>().Val.Value.Equals(tag)).ToList();

               
                return sdtSubTable[0].SdtContentBlock.InnerText;
            }