Re: Trim to 15 Characters by Helmut
Helmut
Fri Jul 22 06:42:02 CDT 2005
Hi Steve,
to MS-Word "Seasons Greetings" are two words.
Whenever you introduce fuzzy natural language concepts
like "word", "syllable" or "sentence", you might find
yourself disappointed with what your code does.
Nevertheless, here comes another exercise.
Sub test8765()
ResetSearch ' clear search options
With Selection
.ExtendMode = False ' switch extendmode off, in case it is on
.StartOf unit:=wdStory ' goto start of doc
With .Find
.Text = "<[A-Za-z]@>"
.MatchWildcards = True
While .Execute
With Selection.Range
If Len(.Text) > 15 Then
.Text = Left(.Text, 15)
.Words(1).HighlightColorIndex = wdYellow
End If
Selection.MoveDown unit:=wdLine
Selection.StartOf unit:=wdLine
End With
Wend
End With
If IsLastline Then GoTo done ' thanks to Jonathan
End With
done:
ResetSearch
End Sub
' ---
Public Function IsLastline() As Boolean
' logic by Jonathan West
With ActiveDocument
IsLastline = (.Range.End - .Bookmarks("\Line").End) <= 1
End With
End Function
' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub
Greetings from Bavaria, Germany
Helmut Weber, MVP, WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"