How to delete a section of text? - I have an either/or word section to process

How can you remove one of the text sections in the document attached?
One piece of text displays if a calculation in my code is made and the other piece of text should display if the calculation is another value

Its an either / or calculation that is made in code
I am using the docBuilder method
for replacing fields found in the word document which is working great

I can’t immediately see how to select some text and delete it though?
here is a code sample of what I am using for replacing simple fields in the word doc

docBuilder = New DocumentBuilder(docWordTemplate)
docBuilder.Document.Range.Replace("[opQuoteId]", CStr(objOrder.quoteId), False, False)

Thanks alot


Thanks for your inquiry. I think, you can easily achieve what you need using IF field. Also, I think, it would be better to use mergefield instead of placeholder. I modified your template (please see the attached file). You should press Alt+F9 to see field code in the document.
Here is code I used for testing:

// Open document.
Document doc = new Document(@"Test001\DeletingText.doc");
// Execute mail merge.
doc.MailMerge.Execute(new string[] { "test" }, new object[] { "1" }); // Condition will be true.
// doc.MailMerge.Execute(new string[] { "test" }, new object[] { "2" }); // Condition will be false.
// Save output.

Hope this helps.
Best regards,

Thanks for your quick reply
The mail merge is one I am considering now
Do you think it would be possible if I created tags to identify the start and end sections, then search for these and then pass the search result to a range to delete it?
eg something like this in the word document:
[start section] my text [end section]
then in the I need these type of commands:'[start section]' & wild set of characters & '[end section]')

make this the active range some how docBuilder.range.delete ?



Thanks for your inquiry. If you need to remove part of content programmatically, I think it would be easier to achieve using bookmark. Just put content that is supposed to be removed into a bookmark and use code like the following to remove the bookmarked content:

doc.Range.Bookmarks["MyBookmark"].Text = "";

Hope this helps.
Best regards,

Can we use a regular expression replace command?

docBuilder.Document.Range.Replace(new Regex("[start text] ^ [end text]"), "")

The only problem is I am not sure what to replace the ^ with ie to search for anything between the 2 tags?

Hi Timothy,
Thanks for your inquiry.
You can use a simple Regex like below:

doc.Range.Replace(new Regex("StartText.*EndText"), new ReplaceEvaluator(), false);

However since most likely the matching text will span over many Run nodes even many paragraphs you will need to use a ReplacingCallback in order to handle what you are trying to achieve.
There is no way to get a “range” from the selected text unless you use the technique from Alexey’s suggestion and insert a new bookmark around the matching text in the ReplaceEvaluator and then set the text to empty. You could also do this without inserting a bookmark by using the code from this page here in your replacing logic then you can easily interact with the matching Run nodes. In your case instead of highlighting the matching nodes you will want to remove them.


Thanks for your inquiry. There is no simple way to remove/replace content between placeholders. However, you can try using the technique suggested in the following forum thread:
Hope this helps.
Best regards,