
Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> 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
[quoted text clipped - 63 lines]
>>>>>
>>>>> Thanks!
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.

Signature
Cheers!
The Kiwi Koder
> Sub Scratchmacro()
> Dim oBM As Bookmark
[quoted text clipped - 86 lines]
> >>>>>
> >>>>> Thanks!
Greg Maxey - 15 Nov 2007 13:06 GMT
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

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> Greg,
>
[quoted text clipped - 141 lines]
>>>>>>>
>>>>>>> Thanks!