![](/img/trans.png)
[英]The name 'c' does not exist in the current context using OpenXml-sdk and C# for accessing Word file
[英]OpenXML SDK C#: How to Copy Bookmark content from 1 word file to another word file
我需要在 C# 中使用 OpenXMl SDK 创建 word 文件,我有2-word files
我需要将一些书签内容从1st-word file
复制到2nd-word file
并保存文件。
提前致谢。
我尝试了不同的方式,下面是我的代码。 我收到错误消息,因为“无法插入 OpenXmlElement‘newChild’,因为它是树的一部分。”
public void testingExecute()
{
try
{
using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1,false))
using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
{
BookmarkStart bookmarkStart = new BookmarkStart();
var res = from bm in wordDoc1.MainDocumentPart.RootElement.Descendants<BookmarkStart>()
where bm.Name == "TestBookmark"
select bm;
var bookmarkstart = res.SingleOrDefault();
var res1 = from bm in wordDoc1.MainDocumentPart.RootElement.Descendants<BookmarkEnd>()
where bm.Id == bookmarkstart.Id
select bm;
var bookmarkend = res1.SingleOrDefault();
body.InsertBeforeSelf<BookmarkStart>(bookmarkstart);
body.InsertAfterSelf<BookmarkEnd>(bookmarkend);
wordDoc2.MainDocumentPart.Document.Save();
}
catch (Exception Ex)
{
throw;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.