Removing Bookmark: Cannot insert a node of this type at this location

I am trying to loop a list of bookmarks to remove their content from a document. The troublesome bookmark seems to be execperks4. In the attached sample code, bookmarksToDelete would be all the bookmarks this document should have deleted. Since execperks4 seems to be bad, I removed all except that and still get the problem.

Any help would be appreciated.

Hi Terry,

Thanks for your inquiry. In this case, I suggest you please use RemoveBookmarkWithContent method. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "Generated+AcmeStmt.doc");
var bookmarksToDelete = new[] { "execperks4" };
foreach (var b in bookmarksToDelete)
{
    var mark = doc.Range.Bookmarks[b];
    RemoveBookmarkWithContent(mark);
}
doc.Save(MyDir + "Out.docx");

You say ‘in this case’…is there a downside to ALWAYS using this method? Or do I have to some how determine if I should use this method vs bookmark.Text = “”?

So I can remove the bookmark without error now, but this method removed blank lines that are included at the start/end of the bookmark. As I tried to describe in this post: Proper way to remove bookmark and content? (item #1 in my post).
I will continue to look for workarounds. Please let me know if you think of any.

Hi Terry,

Thanks for your inquiry. You can workaround the shared issue by using following code example. Hope this helps you.

Document doc = new Document(MyDir + "Generated+AcmeStmt.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
var bookmarksToDelete = new[] { "execperks4" };
foreach (var b in bookmarksToDelete)
{
    var mark = doc.Range.Bookmarks[b];
    if (mark.BookmarkEnd.ParentNode.ChildNodes.Count == 1)
    {
        builder.MoveTo(mark.BookmarkEnd);
        Body body = (Body)mark.BookmarkEnd.GetAncestor(NodeType.Body);
        body.InsertAfter(new Paragraph(doc), mark.BookmarkEnd.ParentNode);
    }
    RemoveBookmarkWithContent(mark);
}
doc.Save(MyDir + "Out.docx");

Still didn’t work. Two things:
a) Given a bookmark, how can I select the ‘Range’ of the entire bookmark? So I can set properties on the font/text?
b) Will go read through RemoveBookmarkWithContent more thoroughly and see if I can spot a problem with it.

Hi Terry,

Thanks for your inquiry.

*terry.aney:

Still didn’t work.*

Please manually remove the bookmark ‘execperks4’ from your document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

*terry.aney:

a) Given a bookmark, how can I select the ‘Range’ of the entire bookmark? So I can set properties on the font/text?*

You need to iterate through all nodes between BookmarkStart and BookmarkEnd. You can use Node.NextPreOrder to get the next node according to the pre-order tree traversal algorithm.

*terry.aney:

b) Will go read through RemoveBookmarkWithContent more thoroughly and see if I can spot a problem with it.*

It would be great if you please share some detail about this issue along with problematic document and expected output document. We will then provide you more information on this along with code.