We are using the DocumentBuilder.InsertHtml() to insert a HTML table into a bookmark in a Word document, and this works just fine. However, when we regenerate the Word document, we want to delete the existing table and insert a new version of the table into the same bookmark. But when we use the command bookmark.Text = string.empty, it does not clear the bookmark. So instead we get two tables instead of one.
See the attached word document for an example.
So the ultimate question is, is there in any way possible to delete this table? Or is there another way of inserting the HTML which makes is possible to delete it later?
doc = new Document(@“c:\test\input.docx”);
Thanks for quick reply. It guided me on the right path.
But I have another question. If I use
var bookmark = documentBuilder.Document.Range.Bookmarks[mappingCellItem.BookMarkName];
I know that the table is now the previousSibling
var table = bookmark.BookmarkStart.ParentNode.PreviousSibling;
But can I be 100% sure that this table belongs to the bookmark “bm”? Or is there another way of getting a reference to the table for the bookmark “bm”?
Thanks for your replies.
We do not create the bookmarks dynamically, we use a word-template with predefined bookmarks.
I think our problem is that when using .InsertHtml(), the table is not inserted inside the bookmark and can therefore (as Awais says) not be deleted directly.
We believe the solution to this problem is to get some kind of reference to the table, so that we can make sure that we are deleting the right table.
Or is there another option?
As mentioned earlier our problem has been that when using .InsertHtml, the table is not inserted inside the bookmark and we could not delete it by using bookmark.Text = string.Empty;. We just found that if we insert a linebreak before the bookmark in our word-template, we can actually delete the table by using bookmark.Text = string.Empty;.
Then everything seems to be working perfectly, but we don`t really understand why.
Thanks for your request. Have you tried using code like this?
public void Test001()
Document doc = new Document(@"c:\temp\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("bm", true, true);
public void Test002()
Document doc = new Document(@"c:\temp\out.docx");
doc.Range.Bookmarks["bm"].Text = "";
This code seems to work as expected on my side.