We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Bookmark problem

I am having a wierd problem with deleting a bookmark – wierd because I’m deleting literally 100’s of others without a hitch, but this one is failing in a way I don’t understand.

Here is a code fragment:
If docNode.NodeType = NodeType.BookmarkStart Then
If docNode.ParentNode.NodeType = NodeType.Paragraph Then
Dim parent As Aspose.Words.Paragraph = docNode.ParentNode

CType(docNode, BookmarkStart).Bookmark.Remove()
If parent.ChildNodes.Count = 0 Then
End If


End If

Examining the HTML, I find

- <w:r w:rsidR="002A6CB9">
- <w:rPr>
<w:sz w:val="18" />
<w:szCs w:val="18" />
<w:t xml:space="preserve"></w:t>
<w:bookmarkStart w:id="200" w:name="ltProratedEquipment" />
<w:bookmarkEnd w:id="200" />
- <w:r w:rsidR="002A6CB9">
- <w:rPr>
<w:sz w:val="18" />
<w:szCs w:val="18" />
<w:t xml:space="preserve"></w:t>

With the bookmarkStart and bookmarkEnd nodes immediately adjacent to each other, and same id. Just like most every other position bookmark.

When I look at the properties in VS2008 debugger, everything looks right: the Bookmark property of the node has a BookmarkStart and a BookmarkEnd, both with the name "ltProratedEquipment" and no text. But when I execute the docNode.Bookmark.Remove() function, the properties in the debugger shows that the Bookmark.BookmarkEnd property now has the Message "Exception 'Cannot find bookmark 'ltProratedEquipment' in the document.' occured in Aspose.Words for .NET
For free technical support, please post this error and the file in the Aspose.Words Forums http://www.aspose.com/forums/ShowForum.aspx?ForumID=75." However, it does not throw an exception. When I make the next pass through the loop, the BookmarkEnd node is still there, and if I try to delete it, it does throw an exception saying it cannot delete it because it has no parent.

I do not understand what is going on and why this book mark is behaving differently from all the others.

Can somebody help?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. If you just would like to remove bookmarks with bookmarked text from the document, you can try using the following code:

'Open document

Dim doc As Document = New Document("C:\Temp\in.doc")

'Remove bookmarks text

For Each bk As Bookmark In doc.Range.Bookmarks

bk.Text = String.Empty


'Remove bookmarks


'Save the document


Also, please attach your document for testing. I will try to reproduce the problem and provide you more information.

Best regards.

Thanks for the reply, Alexey.

I don't want to remove ALL bookmarks, so doc.Range.Bookmarks.Clear() would not be appropriate.

Our template is set up with bookmarks marking named regions of text, bounded by bookmarks of the form "ShowStart" and "ShowEnd". We then drive document production from a database table that has boolean columns of the form "Show". If the column is false, then we want to delete everything from the "ShowStart" bookmark to the "ShowEnd" bookmark, which may include other named regions and/or other bookmarks. The ltProratedEquipment bookmark in question is one such included bookmark.

As I stated, this code has successfully deleted dozens, if not hundreds of other bookmarks in this template, but this one in particular is giving an exception, and I cannot understand why the "BookmarkStart.Bookmark.Remove()" function is failing to remove it. I would be happy to send you the template in question by private email or other secure method.


I think I have resolved the problem. It was a programming error on my part.

At the top of the processing loop to delete a region, I set
nextNode = docNode.NextPreOrder()

and at the bottom, I would set

docNode = nextNode

But the problem occurs when docNode is BookmarkStart node of an empty bookmark, because docNode.NextPreOrder() is then the BookmarkEnd node for that same bookmark, and when you delete the bookmark, you’re now left with nextNode referencing a Node that has already been deleted.

So, I took care of that program, and I think it’s working okay.

(The reason it wasn’t showing up for the other bookmarks is because they were deleted as a result of deleting a composite node that contained them.)

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

It is nice that you have already found the reason of the problem independently.

Best regards,

Thanks for your help. I appreciate having you guys there.