Looping to end of document

R

Rich W.

I have the code . . .

Do
..
..
..
Loop Until ActiveDocument.Bookmarks("\Sel") =
ActiveDocument.Bookmarks("\EndOfDoc")

The point is searching for an opening brackent "[" while selecting text
between that and a closing bracket"]". This seems straight forward.

BUT my loop continues to loop. What's wrong with it? I was trying to find a
more intuitive way of breaking out of the loop, such as
Do . . . .Loop Until ActiveDocument.End

I'm open to any and all suggestions!

(Obviously I'm a novice . . . any help is really appreciated!)

Thanks,

Rich
 
J

Jay Freedman

I have the code . . .

Do
.
.
.
Loop Until ActiveDocument.Bookmarks("\Sel") =
ActiveDocument.Bookmarks("\EndOfDoc")

The point is searching for an opening brackent "[" while selecting text
between that and a closing bracket"]". This seems straight forward.

BUT my loop continues to loop. What's wrong with it? I was trying to find a
more intuitive way of breaking out of the loop, such as
Do . . . .Loop Until ActiveDocument.End

I'm open to any and all suggestions!

(Obviously I'm a novice . . . any help is really appreciated!)

Thanks,

Rich

Hi Rich,

Don't feel bad about it -- I think every novice at Word VBA gets burned at least
once by the mistake of running a loop "until the end of the document". One
problem with it is that you have no way to ensure that the Selection ever gets
to exactly the end of the document -- it could be off by just one character and
it still won't match.

A more immediate problem with the code you posted is that the loop condition
will never be true: one bookmark object never 'equals' another bookmark object
unless they are in fact the _same object_. You should be testing the condition

ActiveDocument.Bookmarks("\Sel").Range.End =
ActiveDocument.Bookmarks("\EndOfDoc").Range.End

which compares the locations of the ends of the two bookmarks. But it would be
better to abandon the whole idea of "until the end of the document".

Instead, use the power of the Find object, especially when used with wildcards
(http://www.gmayor.com/replace_using_wildcards.htm). You can find every
occurrence of text between brackets with the expression

\[*\]

Depending on what you want to do with these occurrences, you may be able to do
it with a single ReplaceAll, or you may need to loop while .Find.Execute returns
True (indicating that it found another occurrence).

For the former case, you would use code something like this:

Sub demo1()
' Find all text surrounded by brackets [ ] and
' change its color to red.

Dim myRg As Range

Set myRg = ActiveDocument.Range

With myRg.Find
.MatchWildcards = True
.Text = "\[*\]"
.Replacement.Text = "^&" ' the found text
.Replacement.Font.Color = wdColorRed
.Execute Replace:=wdReplaceAll
End With
End Sub

For the latter case, you would use code something like this:

Sub demo2()
' Find text surrounded by brackets [ ].
' If that text contains the word "with",
' change its color to red.

Dim myRg As Range

Set myRg = ActiveDocument.Range

With myRg.Find
.MatchWildcards = True
.Text = "\[*\]"
Do While .Execute
If InStr(myRg.Text, "with") > 0 Then
myRg.Font.Color = wdColorRed
End If
Loop
End With
End Sub
 
R

Rich W.

Thanks Jay . . .

I see the error of my ways! I had to try your first "fix"

(ActiveDocument.Bookmarks("\Sel").Range.End =
ActiveDocument.Bookmarks("\EndOfDoc").Range.End)

It worked. But, now, I also see why the Find and Replace method is overall a
better way to do it. I'll implement that.

Thanks again!

Rich W.



Jay Freedman said:
I have the code . . .

Do
.
.
.
Loop Until ActiveDocument.Bookmarks("\Sel") =
ActiveDocument.Bookmarks("\EndOfDoc")

The point is searching for an opening brackent "[" while selecting text
between that and a closing bracket"]". This seems straight forward.

BUT my loop continues to loop. What's wrong with it? I was trying to find a
more intuitive way of breaking out of the loop, such as
Do . . . .Loop Until ActiveDocument.End

I'm open to any and all suggestions!

(Obviously I'm a novice . . . any help is really appreciated!)

Thanks,

Rich

Hi Rich,

Don't feel bad about it -- I think every novice at Word VBA gets burned at least
once by the mistake of running a loop "until the end of the document". One
problem with it is that you have no way to ensure that the Selection ever gets
to exactly the end of the document -- it could be off by just one character and
it still won't match.

A more immediate problem with the code you posted is that the loop condition
will never be true: one bookmark object never 'equals' another bookmark object
unless they are in fact the _same object_. You should be testing the condition

ActiveDocument.Bookmarks("\Sel").Range.End =
ActiveDocument.Bookmarks("\EndOfDoc").Range.End

which compares the locations of the ends of the two bookmarks. But it would be
better to abandon the whole idea of "until the end of the document".

Instead, use the power of the Find object, especially when used with wildcards
(http://www.gmayor.com/replace_using_wildcards.htm). You can find every
occurrence of text between brackets with the expression

\[*\]

Depending on what you want to do with these occurrences, you may be able to do
it with a single ReplaceAll, or you may need to loop while .Find.Execute returns
True (indicating that it found another occurrence).

For the former case, you would use code something like this:

Sub demo1()
' Find all text surrounded by brackets [ ] and
' change its color to red.

Dim myRg As Range

Set myRg = ActiveDocument.Range

With myRg.Find
.MatchWildcards = True
.Text = "\[*\]"
.Replacement.Text = "^&" ' the found text
.Replacement.Font.Color = wdColorRed
.Execute Replace:=wdReplaceAll
End With
End Sub

For the latter case, you would use code something like this:

Sub demo2()
' Find text surrounded by brackets [ ].
' If that text contains the word "with",
' change its color to red.

Dim myRg As Range

Set myRg = ActiveDocument.Range

With myRg.Find
.MatchWildcards = True
.Text = "\[*\]"
Do While .Execute
If InStr(myRg.Text, "with") > 0 Then
myRg.Font.Color = wdColorRed
End If
Loop
End With
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 

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