End of selection = end of doc ... how?

C

Chuck

Hi ther

Any ideas how I can determine whether the end of a selection is at the end of a document

I'm writing a sub that crawls through documents looking for italicised text that meets certain parameters. The sub first searches for an italicised open square bracket (" [ ") then uses Selection.MoveRight to extend the selection looking for the next non-italicised character then goes backwards looking for an italicised close square bracket (" ] "), then bolds the selection. I can't globally replace with wildcards because there may be multiple instances of open-close square brackets in any block of italicised text, and there may be instances where the last close square bracket in the selection is followed by italicised text that should not be bolded. (I've tried the global replace with wildcards and it doesn't do what I need)

The sub works fine unless the last character of the document is italicised in which case the sub hangs at a Selection.MoveRight method.

How can I stop the loop hanging if it tries to .MoveRight at the end of the document? I'm wondering if there's a way to test whether the selection has already been extended to the last character of a document before trying to .MoveRight again? Alternatively if someone has a better suggestion for achieving what I'm trying to do please let me know

Here's the relevant loop in the sub (the entire sub is at the bottom of this message). It hangs at the line commented '*HANGS HERE*

With Selectio

Do Until .Characters(Selection.Characters.Count).Italic = Fals
If .Characters(Selection.Characters.Count).Italic = wdUndefined Or
.Characters(Selection.Characters.Count).Italic = True The
.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend '*HANGS HERE
Els
'do nothin
End I
Loo

End Wit

Here's the whole sub

Sub BoldItalSquareBracketText(

On Error GoTo errorhandle

Dim
Dim lngChars As Lon

lngChars = ActiveDocument.ComputeStatistics(wdStatisticCharacters

Selection.Find.ClearFormattin
Selection.Find.Font.Italic = Tru
Selection.Find.Font.Bold = Fals
With Selection.Fin
.Text = "[
.Replacement.Text = "
.Forward = Tru
.Wrap = wdFindContinu
.Format = Tru
.MatchCase = Fals
.MatchWholeWord = Fals
.MatchWildcards = Fals
.MatchSoundsLike = Fals
.MatchAllWordForms = Fals
End Wit
Selection.Find.Execut

With Selectio

Do Until .Characters(Selection.Characters.Count).Italic = Fals

If .Characters(Selection.Characters.Count).Italic = wdUndefined Or
.Characters(Selection.Characters.Count).Italic = True The

.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExten
Els
End I

Loo

If .Characters(Selection.Characters.Count).Text <> "]" The

.MoveEndUntil Cset:="]", Count:=wdBackwar

Els

End I

.Font.Italic = Tru
.Font.Bold = Tru

i = .Characters.Coun

.Collaps

.Move unit:=wdCharacter, Count:=

End Wit

MsgBox Selection.Tex

Macro

Exit Su

errorhandler

MsgBox Err.Descriptio

End Su
 
C

Chuck

PS to my previous message -- oops

Posted wrong version of the entire sub in my previous message. Correct version appears below

And forgot to include thanks-in-advance for any assistance! ;-

Here's the entire sub

Sub BoldItalSquareBracketText(

On Error GoTo errorhandle

Dim
Dim lngChars As Lon

lngChars = ActiveDocument.ComputeStatistics(wdStatisticCharacters

Selection.Find.ClearFormattin
Selection.Find.Font.Italic = Tru
Selection.Find.Font.Bold = Fals
With Selection.Fin
.Text = "[
.Replacement.Text = "
.Forward = Tru
.Wrap = wdFindSto
.Format = Tru
.MatchCase = Fals
.MatchWholeWord = Fals
.MatchWildcards = Fals
.MatchSoundsLike = Fals
.MatchAllWordForms = Fals
End Wit

Do While Selection.Find.Execute = Tru

With Selectio

Do Until .Characters(Selection.Characters.Count).Italic = Fals

If .Characters(Selection.Characters.Count).Italic = wdUndefined Or
.Characters(Selection.Characters.Count).Italic = True The

.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExten
Els
End I

Loo

If .Characters(Selection.Characters.Count).Text <> "]" The

.MoveEndUntil Cset:="]", Count:=wdBackwar

Els

End I

.Font.Italic = Tru
.Font.Bold = Tru

i = .Characters.Coun

.Collaps

.Move unit:=wdCharacter, Count:=

End Wit

BoldItalSquareBracketTex

Loo

Exit Su

errorhandler

MsgBox Err.Descriptio


End Su
 
J

Jezebel

You can test whether you're at end of document in several ways. Simple is
the Range.End property:

If .Characters(Selection.Characters.Count).End = ActiveDocument.Range.End
then
.... last character in document
 
P

Peter Hewett

Hi Chuck

You can use this function:
Private Function EndOfDocument() As Boolean
EndOfDocument = (Selection.Type = wdSelectionNormal And _
Selection.End = ActiveDocument.Content.End)
End Function

HTH + Cheers - Peter
 

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