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
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