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!

Copying a bookmark name along with its contents

Status
Not open for further replies.

pmcdaniel

Programmer
Feb 9, 2007
127
US
I have the following code inside a VB 6 app.:
Code:
BKSource.Bookmarks("CopyMe").Range.Copy
Set RangeInsertAfter = BKDestination.Bookmarks("PasteAfterMe").Range
RangeInsertAfter.Collapse Direction:=wdCollapseEnd
RangeInsertAfter.Paste
This copies the entire contents of the bookmark including the variables which is great, however, it does not include the bookmark name.
I also tried:
Code:
Set RangeInsertAfter = BKDestination.Bookmarks("PasteAfterMe").Range
RangeInsertAfter.Collapse Direction:=wdCollapseEnd
BKSource.Bookmarks.Application.Selection.GoTo What:=wdGoToBookmark, Name:="CopyMe"
BKSource.Bookmarks.Application.Selection.Copy
RangeInsertAfter.Paste
I played with the above code by moving lines around with the same results. I tried InsertAfter but that did not preserve the bookmark name, variables or the formatting.

Here is how the destination doc and BKs look:
Some meaningless text here.
[Here is more text and I want to insert the new BK after this BK in a new paragraph.
paragraph mark]
[Here is yet more text where I want the new BK to insert before this BK.
paragraph mark]

Here is how the destination doc and BKs look after the insert:
Some meaningless text here.
[Here is more text and I want to insert the new BK after this BK in a new paragraph.
paragraph mark]
The new text is inserted beautifully along with formatting, etc... but the bookmark name is missing.
paragraph mark
[Here is yet more text where I want the new BK to insert before this BK.
paragraph mark]

Any help/advice will be greatly appreciated.
 
Hi pmcdaniel,

Your new bookmark will have an index value equal to the previous highest index value plus one.

Adding a couple of lines of code to hold the old highest value and then adding one each time you add a new bookmark should allow you to rename them post insert I think.

Cheers, Iain
 
Scratch that, name is read only, duh.

Would it be better to use Bookmarks.Add, then set the text?
 
Thanks for the reply, Iain. I tried the Bookmarks.Add using the name and range of the BK being copied but it did not work. Said it didn't exist, which I'm assuming is due to it not existing in the destination document.

How would I be able to set the range of the bk in the destination doc?
 
How about pasting the text from the insertion point, then selecting what you've pasted?

If you do this you can add using Bookmarks.Add("MyBmk",Selection)
 
I thought of that but how do I select the text I just pasted? This is in a VB Application.
 
If you work out how long it is before pasting:

dim intLen as integer

intLen = len(Selection.Text)

you can go back to your insertion point, then use:

Selection.MoveRight Unit:=wdCharacter, Count:= intLen, Extend:=wdExtend

to select the text
 
Thanks for the help, idbr, incredibly enough I fixed it by simply adding the line
Code:
RangeInsertAfter.Bookmarks.Add strBookmark
after the paste method. Evidently the text must be highlighted after pasting.
 
This comes from the use of Selection and copy, rather than Range.

"Evidently the text must be highlighted after pasting. "

No, it is because the .Add method requires a range parameter, and the default is Selection.Range. But it most certainly does not have to be Selection. If you do NOT explicitly use a range, then - as stated - .Add will use Selection.Range (i.e. what is highlighted, or selected).

Gerry
 
Hi pmcdaniel,

Here's some code I've used to generate a list of all bookmarks at the end of the active document, and display their contents:
Code:
Sub ListBkMrks()
Dim oBkMrk As Bookmark
If ActiveDocument.Bookmarks.Count > 0 Then
  With Selection
    .EndKey Unit:=wdStory
    .TypeText Text:=vbCrLf & "Bookmark" & vbTab & "Contents"
    For Each oBkMrk In ActiveDocument.Bookmarks
      .TypeText Text:=vbCrLf & oBkMrk.Name & vbTab & oBkMrk.Range.Text
    Next oBkMrk
  End With
End If
End Sub
It should be easy enough to modify this to suit your needs.

If you need to preserve the source formatting, you could simply:
. insert a cross-reference field to the original bookmark (only if inserting into the same document), or
. copy the original range and paste it wherever you need it (eg in another document),
instead of explicity typing its contents (which my code does via '& oBkMrk.Range.Text'.

Cheers


[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top