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 9109539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:03:58+00:00 2026-06-17T03:03:58+00:00

I am trying to edit a word doc using openxml and c#. I have

  • 0

I am trying to edit a word doc using openxml and c#. I have the following xml structure for the doc:

    <w:bookmarkStart w:name="HelloWorld" w:id="0" />
    <w:bookmarkStart w:name="_GoBack" w:id="1" />
    <w:r w:rsidRPr="00874DDF">
      <w:rPr>
        <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
        <w:b />
        <w:bCs />
        <w:sz w:val="28" />
        <w:szCs w:val="28" />
        <w:u w:val="single" />
      </w:rPr>
      <w:t>Hello World:</w:t>
    </w:r>
  </w:p>
  <w:p w:rsidRPr="00583A84" w:rsidR="00CB71E3" w:rsidP="00CB71E3" w:rsidRDefault="00CB71E3">
    <w:pPr>
      <w:spacing w:line="260" w:lineRule="atLeast" />
      <w:jc w:val="both" />
      <w:rPr>
        <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
        <w:color w:val="000000" />
      </w:rPr>
    </w:pPr>
    <w:r w:rsidRPr="00583A84">
      <w:rPr>
        <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
        <w:color w:val="000000" />
      </w:rPr>
      <w:t>HelloWorld.</w:t>
    </w:r>
  </w:p>
  <w:bookmarkEnd w:id="0" />
  <w:bookmarkEnd w:id="1" />

I am trying to delete all the text in a bookmark in my word doc page. The xml structure however is making it impossible for me to do so. This is because the bookmark text is split between two different paragraph parent tags. How can i delete all the text between bookmark start 0 and bookmark end 0?

  • 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-06-17T03:03:59+00:00Added an answer on June 17, 2026 at 3:03 am

    I’m building a small project that edits text, tables and graphs in Word at my job.
    Using openXML this is how I would go about replacing text covered by a bookmark.

    I do not know whether you have the isolation of the bookmarkStart element covered, so I included that as well.

    First the code for capturing the bookmarkStart:

    Dictionary<String,BookmarkStart> bookMap = new Dictionary<String, BookmarkStart>(); //a dictionary so we can lookup a bookmarkStart with it's name
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open("MyWordDocument.docx", true))
    {
        var mainPart = wordDoc.MainDocumentPart; //get mainpart
        var bookmarks = mainPart.Document.Body.Descendants<BookmarkStart>(); //get all bookmarks
        foreach (BookmarkStart bookmarkStart in bookmarks) //save them to dictionary
        {   
            bookMap[bookmarkStart.Name] = bookmarkStart;
        }
        ReplaceInBookmark(bookmark["myBookmark"],"text to insert"); 
    }
    

    Then the replacing part:

    public  void ReplaceInBookmark(BookmarkStart bookmarkStart, string text)
            {
                OpenXmlElement elem = bookmarkStart.NextSibling();
                while (elem != null && !(elem is BookmarkEnd))
                {
                    OpenXmlElement nextElem = elem.NextSibling();
                    elem.Remove();
                    elem = nextElem;
                }
                bookmarkStart.Parent.InsertAfter<Run>(new Run(new Text(text)), bookmarkStart);
            }
    

    This, I think, should do the job.
    But it’s a copypaste with some small edits of some code I’m not at liberty to test right now, so bugs/mistakes may be present.

    If you want to delete the bookmark, and it’s content (not replacing it), you can either try to send an empty String or make a small change in the method.
    This one basically removes all the elements and then inserts a new Run with the text specified.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to edit a word document from VB.NET using for the most
I have the following code: using (var doc = WordprocessingDocument.Open(filename, true)) { .... }
I'm trying to edit a form with a ImageField and I´m using modelformset_factory in
I am trying to edit the wikitext of a page using a textView and
I'm trying to edit header of a MS Word document that has existing header
I'm trying to edit a .docx header through Word Automation. If the Content Controls
I'm trying to create a fairly simple macro in Word 2010 using Visual Basic.
EDIT : You're not allowed to have the word question in the title. Q-mark
I am trying to Split every character of a word..Such as CAT=C,A,T I have
i have an algorithm that generates permutations of a given word. I'm trying to

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.