Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy/paste nested bookmarks from one document to another

Status
Not open for further replies.

pmcdaniel

Programmer
Feb 9, 2007
127
US
Is there a way to copy nested bookmarks from one document to another? The BK does not exist in the Destination document.
For instance:
[Here is an example [sentence].]

Code:
BKSource.Bookmarks(strBookmark).Range.Copy
Set RangeDestination = BKDestination.Bookmarks(strBookMarkInsertAfter).Range
RangeDestination.Collapse Direction:=wdCollapseEnd
RangeDestination.Paste
RangeDestination.Bookmarks.Add strBookmark

End result:
[Here is an example sentence.]

I tried BKSource.Bookmarks(strBookmark).Range.Select and then BKSource.Bookmarks(strBookmark).Range.Copy but that did not help.

I can successfully copy the outer bk but the inner bk is gone.

Please help.
 
I am confused. On one hand you write:

"The BK does not exist in the Destination document"

on the other hand, your code has:
Code:
Set RangeDestination = BKDestination.Bookmarks(strBookMarkInsertAfter).Range
How can you set a Range for a bookmark range that does not exist?

In any case, perhaps you can adjust the following code for your purposes.

I made a document with precisely your example.

[Here is an example [sentence].]

The whole paragraph is bookmarked as "WholeThing."

"sentence" - the inside/nested bookmark - is named "Nested".
Code:
Sub CopyNestedBookmark()
Dim oSourceBM As Bookmark
Dim oDestinationBookmark As Bookmark
Dim r As Range

Set oSourceBM = ActiveDocument.Bookmarks("WholeThing")

Set r = oSourceBM.Range
r.Copy
   
Documents.Add
Set oDestinationBookmark = _
   ActiveDocument.Bookmarks.Add("WholeThing", _
   Range:=Selection.Range)
With oDestinationBookmark.Range
   .Collapse 0
   .Paste
   .Bookmarks.Add "WholeThing"
End With

End Sub
The code creates a new document, and copies over the bookmark "WholeThing", including the nested bookmark "Nested".

The new document looks like:

[Here is an example [sentence].]

The difference is that the code works with bookmark objects themselves.

Gerry
 




Gerry,

I'd like to see what you can do with a

WholeNutherThing

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top