0
我已经编写了下面的代码,在文档的页脚的contentcontrol中插入一些文本。OpenXML插入到内容控件中丢失Word.Text
oItem.File.CheckOut();
byte[] byteArray = oItem.File.OpenBinary();
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument wp = WordprocessingDocument.Open(mem, true))
{
Boolean foundInFooter = false;
MainDocumentPart mainPart = wp.MainDocumentPart;
foreach (FooterPart footerPart in mainPart.FooterParts)
{
Word.Footer footer = footerPart.Footer;
foreach (Word.SdtElement sdt in footer.Descendants<Word.SdtElement>().ToList())
{
Word.SdtAlias alias = sdt.Descendants<Word.SdtAlias>().FirstOrDefault();
if (alias.Val.Value == "Revisionsnummer")
{
foundInFooter = true;
if (sdt.Descendants<Word.Text>().FirstOrDefault() != null)
{
sdt.Descendants<Word.Text>().FirstOrDefault().Text = (string)oItem["Version"];
}
}
}
}
}
}
出于某种原因有时sdt.Descendants<Word.Text>().FirstOrDefault()
返回NULL,所以我不能插入文本。无论如何,在这些情况下添加Word.Text?
我的问题也许不清楚,但即时询问是什么,我应该把其他语句,如果obj == null插入文本内容控制? – 2013-05-07 13:46:32
它应该可以做,因为我张贴因为我首先检查空值,但什么在别的声明是我的问题希望你明白:) – 2013-05-07 13:49:19