goto next bookmark in the document

A

Antb

Hi,

I am looking to cut and paste text between 2 bookmarks. I can specify the
identity of the 1st bookmark but not the 2nd. So I’d like to do the following:

Goto bookmark1
Goto the next bookmark in the document
Goto the next bookmark in the document
Select the text between bookmark1 and here
Cut and paste elsewhere

I cannot find the syntax to go to the next bookmark in the document. All the
functions ask me to specify the name of a bookmark.

Please help.

Thanks!
 
P

Pesach Shelnitz

Hi,

The following macro will select the text from the beginning of one bookmark
to the end of the next bookmark. You can add Selection.Cut and code to paste
the selection in the desired place.

Sub SelectConsecBookmarks()
Const Error_BookmarkNotFound = 5941
Dim bkName As String
Dim myBookmark As Bookmark
Dim num As Long

bkName = InputBox("Type a bookmark name.")
Set myRange = ActiveDocument.Range
With myRange
On Error Resume Next
Set myBookmark = .Bookmarks(bkName)
If Err.Number = Error_BookmarkNotFound Then
MsgBox "The bookmark specified was not found."
Exit Sub
End If
On Error GoTo 0
For num = 1 To .Bookmarks.Count
If LCase(.Bookmarks(num).name) = _
LCase(bkName) Then
Exit For
End If
Next
If num < .Bookmarks.Count Then
myBookmark.Select
Selection.End = _
.Bookmarks(num + 1).Range.End
'Add cut/paste code here.
Else
MsgBox "There is no bookmark after the bookmark specified."
End If
End With
Set myBookmark = Nothing
End Sub
 
G

Graham Mayor

With a little bit of digging and credit to Malcolm Smith at dragondrop.com
the following code will identify the next bookmark in the document from the
cursor position (which could be a bookmark) and write the content from the
current position to the next bookmark to oRng.

Option Explicit
Sub FindNextBookmark()
Dim oBookmark As Bookmark
Dim nCurrentPosition As Long
Dim nPosition As Long
Dim nFirstPosition As Long
Dim sFirstBookmark As String
Dim nNextPosition As Long
Dim sNextBookmark As String
Dim oRng As Range
If Documents.Count > 0 Then
Set oRng = Selection.Range
nCurrentPosition = Selection.Range.Start
nNextPosition = 0
nFirstPosition = 0
For Each oBookmark In ActiveDocument.Bookmarks
nPosition = oBookmark.Start
If nFirstPosition = 0 _
Or nPosition < nFirstPosition Then
nFirstPosition = nPosition
sFirstBookmark = oBookmark.name
End If
If nPosition > nCurrentPosition _
And (nPosition < nNextPosition _
Or nNextPosition = 0) Then
nNextPosition = nPosition
sNextBookmark = oBookmark.name
End If
Next oBookmark
If nNextPosition > 0 Then
ActiveDocument.Bookmarks(sNextBookmark).Range.Select
ElseIf nFirstPosition > 0 Then
ActiveDocument.Bookmarks(sFirstBookmark).Range.Select
End If
oRng.End = Selection.Range.End
End If
MsgBox oRng
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

Antb

wow, thank you guys. this looks great!



Graham Mayor said:
With a little bit of digging and credit to Malcolm Smith at dragondrop.com
the following code will identify the next bookmark in the document from the
cursor position (which could be a bookmark) and write the content from the
current position to the next bookmark to oRng.

Option Explicit
Sub FindNextBookmark()
Dim oBookmark As Bookmark
Dim nCurrentPosition As Long
Dim nPosition As Long
Dim nFirstPosition As Long
Dim sFirstBookmark As String
Dim nNextPosition As Long
Dim sNextBookmark As String
Dim oRng As Range
If Documents.Count > 0 Then
Set oRng = Selection.Range
nCurrentPosition = Selection.Range.Start
nNextPosition = 0
nFirstPosition = 0
For Each oBookmark In ActiveDocument.Bookmarks
nPosition = oBookmark.Start
If nFirstPosition = 0 _
Or nPosition < nFirstPosition Then
nFirstPosition = nPosition
sFirstBookmark = oBookmark.name
End If
If nPosition > nCurrentPosition _
And (nPosition < nNextPosition _
Or nNextPosition = 0) Then
nNextPosition = nPosition
sNextBookmark = oBookmark.name
End If
Next oBookmark
If nNextPosition > 0 Then
ActiveDocument.Bookmarks(sNextBookmark).Range.Select
ElseIf nFirstPosition > 0 Then
ActiveDocument.Bookmarks(sFirstBookmark).Range.Select
End If
oRng.End = Selection.Range.End
End If
MsgBox oRng
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Fumei2 via OfficeKB.com

This also gets the text between the cursor position and the START of the next
bookmark.

Sub TextToNextBookmark()
Dim r As Range
Dim Chunk As Range

Set r = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=ActiveDocument.Range.End)

Set Chunk = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=r.Bookmarks(1).Range.Start)

MsgBox Chunk.Text
End Sub

If you want to get the text between the cursor position and the next TWO
bookmarks...

Sub TextToNextBookmark()
Dim r As Range
Dim Chunk As Range

Set r = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=ActiveDocument.Range.End)

Set Chunk = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=r.Bookmarks(2).Range.Start)

MsgBox Chunk.Text
End Sub

Watch ourt for errors though! If there are not two bookmarks ahead of the
cursor - like in the code above - then obviously r.Bookmarks(2) would return
the dreaded "not in the collection" error. This could easily be tested for,
and error-trapped.
wow, thank you guys. this looks great!
With a little bit of digging and credit to Malcolm Smith at dragondrop.com
the following code will identify the next bookmark in the document from the
[quoted text clipped - 58 lines]
 
F

Fumei2 via OfficeKB.com

I just want to add that all of these things:

Goto bookmark1
Goto the next bookmark in the document
Goto the next bookmark in the document
Select the text between bookmark1 and here
Cut and paste elsewhere (except for this instruction)

are just using numbers. The difficulty lies in the way Word indexes
bookmarks. It does it alphabetically. Suppose you have:

Yadda yadda
[This is bookmarked, named ONE]
Yadda yadda
Yadda
[This is bookmarked, named TWO]
Yadda blah
Blah
Blah
[This is bookmarked, named THREE]

Bookmarks(1) = [This is bookmarked, named ONE]
Bookmarks(2) = [This is bookmarked, named THREE]
Bookmarks(3) = [This is bookmarked, named TWO]

The Index of the bookmark Collection is not the order in the document, but
the order alphabetically. Which makes using Index numbers from the bookmarks
collection somewhat problematically, and not - unfortunately -
straightforward.


This also gets the text between the cursor position and the START of the next
bookmark.

Sub TextToNextBookmark()
Dim r As Range
Dim Chunk As Range

Set r = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=ActiveDocument.Range.End)

Set Chunk = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=r.Bookmarks(1).Range.Start)

MsgBox Chunk.Text
End Sub

If you want to get the text between the cursor position and the next TWO
bookmarks...

Sub TextToNextBookmark()
Dim r As Range
Dim Chunk As Range

Set r = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=ActiveDocument.Range.End)

Set Chunk = ActiveDocument.Range( _
Start:=Selection.Start, _
End:=r.Bookmarks(2).Range.Start)

MsgBox Chunk.Text
End Sub

Watch ourt for errors though! If there are not two bookmarks ahead of the
cursor - like in the code above - then obviously r.Bookmarks(2) would return
the dreaded "not in the collection" error. This could easily be tested for,
and error-trapped.
wow, thank you guys. this looks great!
[quoted text clipped - 3 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top