Setting a bookmark by macro

D

Deejay

Please could someone provide the code to do the following?

Take the characters from the right of bookmarkA to the end of the line and
name them bookmarkB.

Thanks!
 
G

Greg Maxey

Something like this:

Sub Scratchmacro()
Dim oBMs As Bookmarks
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
oBMs("bma").Range.Select
Set oRng = ActiveDocument.Bookmarks("\line").Range
oRng.Start = oBMs("bma").End + 1
oRng.End = oRng.End - 1
oBMs.Add "bmb", oRng
MsgBox oBMs("bmb").Range.Text
End Sub
 
D

Deejay

Thank you so so much although the message is not always accurate it does
work. May I just trouble you on one further point. How do I check prior to
running this code whether bookmark "bmb" already exists so it doesn't run it
in vain?

Thanks once again.
 
G

Greg Maxey

Sub Scratchmacro()
Dim oBM As Bookmark
Dim oBMs As Bookmarks
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
For Each oBM In oBMs
If oBM.Name = "bmb" Then
Exit Sub
End If
Next oBM
oBMs("bma").Range.Select
Set oRng = ActiveDocument.Bookmarks("\line").Range
oRng.Start = oBMs("bma").End + 1
oRng.End = oRng.End - 1
oBMs.Add "bmb", oRng
MsgBox oBMs("bmb").Range.Text
End Sub
 
D

Deejay

Thanks again. I hope you'll allow one more query. You put an exit sub command
to get out of it. In my case your code is nested in a larger macro and I need
it to carry on with other things after the line immediately prior to the
msgbox. So how do I get it simply to leave this section. Must I make your
code a second macro and the first one will call on it or is there a way to
have them all in one?

Kind regards
 
G

Greg Maxey

Sub Scratchmacro()
Dim oBM As Bookmark
Dim oBMs As Bookmarks
Dim bAddBM As Boolean
Dim oRng As Word.Range
Set oBMs = ActiveDocument.Bookmarks
bAddBM = True
For Each oBM In oBMs
If oBM.Name = "bmb" Then
bAddBM = False
Exit For
End If
Next oBM
If bAddBM Then
oBMs("bma").Range.Select
Set oRng = ActiveDocument.Bookmarks("\line").Range
oRng.Start = oBMs("bma").End + 1
oRng.End = oRng.End - 1
oBMs.Add "bmb", oRng
MsgBox oBMs("bmb").Range.Text
End If
End Sub
 
N

NZ VBA Developer

Greg,

Rather than looping through the whole bookmarks collection and setting a
Boolean variable, couldn't you just use .Exists like this?

Sub Scratchmacro( )
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
If Not oBMs.Exists("bmb") Then
Dim oRng As Word.Range
oBMs("bma").Range.Select
Set oRng = oBMs("\line").Range
oRng.Start = oBMs("bma").End + 1
oRng.End = oRng.End - 1
oBMs.Add "bmb", oRng
MsgBox oBMs("bmb").Range.Text
End If
End Sub

Or maybe add a similar check (with some feedback) to make sure the "bma"
exists before trying to select it? e.g.:

If Not oBMs.Exists("bmb") Then
If oBMs.Exists("bma") Then
Dim oRng As Word.Range
oBMs("bma").Range.Select
Set oRng = oBMs("\line").Range
oRng.Start = oBMs("bma").End + 1
oRng.End = oRng.End - 1
oBMs.Add "bmb", oRng
MsgBox oBMs("bmb").Range.Text
Else
MsgBox "The 'bma' bookmark is missing."
End If
Else
MsgBox "The 'bmb' bookmark already exist."
End If

Might be quicker and saves a couple of declarations...

BTW, Deejay, don't worry too much about the small differences between my
code and Greg's. They both work equally well. I just cleaned up a couple of
things - made use of the oBMs object throughout and held off creating the
Range object until it was needed. Minor stuff really.

However, I did discover that if the 'bma' bookmarks is on the last line of
the document, the result isn't quite what's expected. Needs a bit of error
handling for this particular case.
 
G

Greg Maxey

You are quite right. To be honest I had forgotten about Exists. I
considered avoiding the loop using error handling:

On Error Resume Next
Set oRng = oBMs("bmb").Range
If Err.Number = 5941 Then
bAddBM = True
End If
On Error GoTo 0

If view of your comments perhaps:

Sub Scratchmacro()
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
If Not oBMs.Exists("bmb") And oBMs.Exists("bma") Then
Dim oRng As Word.Range
oBMs("bma").Range.Select
Set oRng = ActiveDocument.Bookmarks("\line").Range
oRng.Start = oBMs("bma").End + 1
If oRng.End <> ActiveDocument.Range.End - 1 Then
oRng.End = oRng.End - 1
End If
oBMs.Add "bmb", oRng
Else
MsgBox "Bookmark bma does not exist or bookmark bmb already exists"
End If
End Sub
 

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