Free Support Forum - aspose.com

Problem copying sections which contain bookmarks

I have attached a sample project where I clone a section that contains 3 bookmarks, renames the bookmarks and then inserts the new section. I then iterate through all of the document's bookmarks replacing the bookmark's text with the name of the bookmark. This process generates exceptions "Start and end node should have the same grand parent." If you look at the resulting file you will see that the process of cloning and renaming somehow split the bookmarks so the all of the BookmarkStart's are in the first section and all of the BookmarkEnd's are in the second section. This can be viewed using your DocumentExplorer sample application.

Can you show me a better way of copying sections and renaming the embedded bookmarks?

Thanks

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

Thanks for your request. Please try using the following code:

'Clone sections with bookmarks

For Each sec In doc.Sections

If sec.Range.Bookmarks.Count > 0 Then

bm = sec.Range.Bookmarks(0)

If bm.Name Like "begsub*" And bm.Name.IndexOf("_z_") = -1 Then

newSec = sec.Clone(True)

doc.InsertAfter(newSec, sec)

Exit For

End If

End If

Next

'Rename bookmarks in document

For Each sec In doc.Sections

If sec.Range.Bookmarks.Count > 0 Then

For Each bm In sec.Range.Bookmarks

bm.Name = bm.Name & "_z_" & i.ToString()

Next

End If

i += 1

Next

Hope this helps.

Best regards.