Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:16:14+00:00 2026-05-31T15:16:14+00:00

I am looking for a way to insert some text after a bookmark in

  • 0

I am looking for a way to insert some text after a bookmark in a word doc using openxml. So far, i have been able to locate the bookmark using the following:

var bookmarks = mainPart.Document.Descendants<BookmarkStart>().ToList();
var bookMarkToWriteAfter = bookmarks.FirstOrDefault(bm => bm.Name == insertAfterBoomark.Name);

This bookmark in the word doc is a selection of two lines in the doc. I have to insert some text right after the two line selection. I have tried to insert text using the following:

var run = new Run();
run.Append(new Text("Hello World"));
bookMarkToWriteAfter .Parent.InsertAfterSelf(run);

mainPart.Document.Save();

This however does not produce the desired result. Does anyone know of the correct way to insert text right after a bookmark in a word doc using openxml?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:16:15+00:00Added an answer on May 31, 2026 at 3:16 pm

    using

    bookMarkToWriteAfter.Parent.InsertAfterSelf(run);
    

    you are trying to work with XML directly which is not always advisable with OpenXML.

    Try This..

        Body body = mainPart.Document.GetFirstChild<Body>();
        var paras = body.Elements<Paragraph>();
    
        //Iterate through the paragraphs to find the bookmarks inside
        foreach (var para in paras)
        {
            var bookMarkStarts = para.Elements<BookmarkStart>();
            var bookMarkEnds = para.Elements<BookmarkEnd>();
    
    
            foreach (BookmarkStart bookMarkStart in bookMarkStarts)
            {
                if (bookMarkStart.Name == bookmarkName)
                {
                    //Get the id of the bookmark start to find the bookmark end
                    var id = bookMarkStart.Id.Value;
                    var bookmarkEnd = bookMarkEnds.Where(i => i.Id.Value == id).First();
    
                    var runElement = new Run(new Text("Hello World!!!"));
    
                    para.InsertAfter(runElement, bookmarkEnd);
    
                }
            }
       }
       mainPart.Document.Save();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to work out how to insert text before and after
I'm looking for a way to map a simple insert stored procedure using NHibernate
I am using codeigniter and looking a way to enable directly editting of doc
i have some simple SELECT statements and an INSERT wrapped in a using(TransactionScope...) block.
I'm looking for the most efficient way to bulk-insert some millions of tuples into
I have been looking for a way to add the information (string) from a
I am looking the fastest way to insert many records at once (+1000) to
I am looking for a way to insert javascript code block to end of
I'm looking for a way to insert a new entry into a specific row
I have managed to insert the whole text-file data in SQL-SERVER database table with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.