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

Free Support Forum - aspose.com

Bookmark.Text = String.Empty not working for bookmark with child nodes

Hello,

I am trying to change our reporting system which is based on ms word automation. I am pretty happy with the process so far but seem to have hit a showstopper.

I cannot find an equivalent for bookmark.cut of ms word. Just deleting a simple bookmark using the text property work perfectly well.

But if I want a bookmark which has nested elements the approach fails with an exception:

Cannot insert a node of this type at this location.

bei Aspose.Words.CompositeNode.xef23aa45e7612fdd(Node x40e458b3a58f5782, Node xff5adbb92d63bf52, Boolean x7f43f79506f73a73)
bei Aspose.Words.CompositeNode.InsertAfter(Node newChild, Node refChild)
bei Aspose.Words.CompositeNode.x2729186e1a0b4aeb(Node x10aaa7cdfa38f254, Node xca09b6c2b5b18485, Node x22bff10c3dd1f70f)
bei x28925c9b27b37a46.x5699f8523a546a42.x15fc7f7f5bee2386()
bei x28925c9b27b37a46.x5699f8523a546a42.xb4edb0bfdcecb3a5()
bei x28925c9b27b37a46.x5699f8523a546a42.xd993fd34ab5f063d()
bei x28925c9b27b37a46.x5699f8523a546a42.x52b190e626f65140(x7e263f21a73a027a x9b10ace6509508c0, xe1bd913bc72a8d58 x54530ac2e150d822)
bei x28925c9b27b37a46.x5699f8523a546a42.x52b190e626f65140(Node x10aaa7cdfa38f254, Boolean x4ec19a117bbb0963, Node xca09b6c2b5b18485, Boolean xead571f03cb4ee1d, xe1bd913bc72a8d58 x54530ac2e150d822)
bei x28925c9b27b37a46.x5699f8523a546a42.x52b190e626f65140(Node x10aaa7cdfa38f254, Boolean x4ec19a117bbb0963, Node xca09b6c2b5b18485, Boolean xead571f03cb4ee1d)
bei Aspose.Words.Bookmark.set_Text(String value)
bei PODS.DocumentProcessor.CutBookmark(String sbookmarkname) in C:\projekte\Spike\PODS\Modul\DocumentProcessor.vb:Zeile 398.

Is there a way around this problem? How can I remove the nested elements including the text in the most efficient way?

With regards,

Robert

Hi Robert,


Thanks for your inquiry. I think, you can use the following code to be able to remove all nodes contained between BookmarkStart and BookmarkEnd nodes:

Document doc = new
Document(@“C:\Temp\in.docx”);

Bookmark bookmark = doc.Range.Bookmarks["bm"];

Node currentNode = bookmark.BookmarkStart;

bool flag = true;

while (currentNode != null && flag)

{

if (currentNode.NodeType == NodeType.BookmarkEnd)

flag = false;

Node nextNode = currentNode.NextPreOrder(currentNode.Document);

currentNode.Remove();

currentNode = nextNode;

}

doc.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best regards,

Hi Awais,

this works partly for me. I’ve got two tables in my document I need to delete. When I put a bookmark around them, only the first table gets deleted, I don’t understand why. If I have a look at the nodes which are iterated and deleted:

- {Aspose.Words.BookmarkStart}
- {Aspose.Words.Tables.Table}
- {Aspose.Words.Tables.Row}
- {Aspose.Words.Tables.Cell}
- {Aspose.Words.Paragraph}
- {Aspose.Words.Run}
- null

The loop is left because of the null value, but the whole table (with somes rows and columns) gets deleted. I don’t understand why the loop doesn’t reach the BookmarkEnd. But - so far so good, the first table has gone. But after a paragraph sign there is a second table contained in the bookmark which is not deleted.

Have you got any idea why?

Thank you
Ingo

OK, I got it - I had to replace:

Node
nextNode =
currentNode.NextPreOrder(currentNode.Document);

with:

Node nextNode;
if
(currentNode.NodeType == NodeType.Table)

nextNode = currentNode.NextSibling;
else
nextNode = currentNode.NextPreOrder(currentNode.Document);

Regards
Ingo

Hi Ingo,


Thanks for your inquiry. It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,