Help deleting text between two SDT controls

Hi, I am throwing out a question because I just do not know where to start.

I have two SDT controls (Rich text) and I would like to delete all of the text between the controls. How would I go about that? Can I create a range using the SDT?

Any guidance is appreciated,
Best
Mark

While you were all sleeping I asked chatgpt and this was the answer …

// Find the starting and ending SDT nodes between which you want to remove all content
StructuredDocumentTag startSdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 3, true);
StructuredDocumentTag endSdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 7, true);

// Remove all content between the SDT nodes
Node currentNode = startSdt.NextPreOrder(startSdt.Document);
while (currentNode != null && currentNode != endSdt)
{
    Node nextNode = currentNode.NextPreOrder(startSdt.Document);
    currentNode.Remove();
    currentNode = nextNode;
}

I think I am going to retire.

@markchequer In addition to the ChatGPT code, which is quite relevant, the following article might be useful for you:
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/

Yes, I am aware of that example, it does not remove, just copy (nice code), but chatgpt nailed it. Thank you for the response.

1 Like