Deleting text between two bookmarks

Hi all

Hope someone can help me with this one. I have already searched the Forums and documentation, but couldn’t find a solution.

How do I delete text/range between two bookmarks, bookmarkStart and bookmarkEnd?

I used to do it like this in VB6:

Set aRange = aWordDoc.Range( _
Start:=aWordDoc.Bookmarks(“bookmarkStart”).Start, _
End:=aWordDoc.Bookmarks(“bookmarkEnd”).End)
aRange.Delete

Regards

Uffe

For simple cases when only some part of the paragraphtext is bookmarked you can just set the Bookmark.Text property to an empty string (“”). For complex cases please check the source code example given in the end of the following article:

https://docs.aspose.com/words/net/working-with-bookmarks/

Hope this helps,

Thanks a lot for your quick answer.

So there is no simple way to delete text between two different bookmarks? I think your example assumes only one bookmark?

Hi,

It seems like the example works with different bookmarks as well. Just modify the beginning as follows:

private void RemoveBookmarkWithContent(Bookmark startBookmark, Bookmark endBookmark)
{
    // We need to store other bookmark nodes here, to move them away from the removed area.
    Hashtable bookmarkNames = new Hashtable();
    Hashtable bookmarkStarts = new Hashtable();
    Hashtable bookmarkEnds = new Hashtable();
    ArrayList nodesToRemove = new ArrayList();
    BookmarkStart bookmarkStart = startBookmark.BookmarkStart;
    BookmarkEnd bookmarkEnd = endBookmark.BookmarkEnd;
    ....

Please tell me if you succeed to implement the task.

1 Like

Hi

Just tested it and it seems to work fine.

Thanks a lot :slight_smile:

Regards Uffe