Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

Trying to copy bookmark content from 1 word file to another word file using OpenXml SDK C#, Getting error

public void testingExecute()
    {
        try
        {
            using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1,false)) // opening 1st word file
            using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true)) // opening 2nd word file
            {

                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;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...