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.