I seem to be having difficulty copying a section that contains bookmarks, then utilizing the “text” property of the bookmarks (and probably other properties as well):
I have a document with an introductory section, a main section, then a closing section. I would like to use the main section in a loop (effectively copying the main section “X” times), which seems quite straightforward using the section.Clone(true) function… The problem occurs with bookmarks which I access in each section (usually to delete the contents of a given bookmark) - I receive an exception that reads the “start and end node should have the same grand parent”.
If I save out the document without touching the bookmarks I note that the bookmarks seem to stretch from the first section to the final section and only appear once (not once per section), which would seem to correspond with the error message. I saw a prior post which indicated that you could rename the bookmarks, but this doesn’t seem to have any effect (and after renaming, and saving, the “renamed” bookmarks have disappeared!).
I do have a temporary workaround:
- Create a new temporary document in memory
- Import the section to your new document using the “Document.ImportNode” function
- Relabel all the bookmarks (I just append a number to the bookmarks’ name, relatively to the loop/copy I’m on)
- Import the relabelled section from the temporary document back to the main document, again using the “Document.ImportNode” function.
While this works, it seems awfully ugly - am I missing something?