Delete content between 2 merge fields?

Due to your kindly answer of my question, I yesterday downloaded the new version 3 of Aspose.Word.

After trying/search several hours, I still found no way to delete the content between 2 merge fields.

I want to do handle more advanced cases inside a MailMergeField-event handler as you described inside a forum reply.

Question: Is it somehow possible to delete the content between two merge fields (start and end inside)? (or are nested merge-regions supported now?)

Thank you
Uwe

It is possible, although might be a bit complicated in some scenarios because you have to deal a lot with the tree nodes. Lets take this simple case:

Document
Section
Body
Paragraph

Paragraph
FieldStart (FieldType.FieldMergeField)
Run “MERGEFIELD MyField1”
FieldSeparator
Run “<>”
FieldEnd
Paragraph
Run “Blah blah”
Paragraph
FieldStart (FieldType.MergeField)
Run “MERGEFIELD MyField2”
FieldSeparator
Run “<>”
FieldEnd
Paragraph



The above scenario is simple because the merge fields are in standalone paragraphs (there is no other text in the paragraph apart from the merge field) and the paragraphs have the same parent - Body. All you need to do here is to find the start and end paragraphs and delete all nodes in between them.

//Say you know the field start nodes.
FieldStart field1Start = FindMyField(“MyField1”);
FieldStart field1Start = FindMyField(“MyField2”);

Paragraph para1 = field1Start.ParentParagraph;
Paragraph para2 = field2Start.ParentParagraph;

//Sanity check that the loop below can make sense.
Debug.Assert(para1.ParentNode == para2.ParentNode);

Node curNode = para1.NextSibling;
while ((curNode != null) && (curNode != para2))
{
Node nextNode = curNode.NextSibling;
curNode.ParentNode.RemoveChild(curNode);
curNode = nextNode;
}

Thank you!

So this functionality would be the ideal candidate to be made available as a built-in method in a future update of your Aspose.Word library?! Wink

This would hide the complexity you metioned and would allow to be used with all scenarios, even complicated ones…