Trim to 15 Characters

S

Steved

Hello from Steved

Below finds the the first word on a line
Question when found is ti posiible to trim to 15 characters include spaces
in the below macro please.

an example could be "the cat won the show"
it would end up as "the cat won the"

Selection.Find.ClearFormatting
With Selection.Find
.Text = "<[A-Za-z]@>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

Thankyou.
 
H

Helmut Weber

Hi Steve
Below finds the the first word on a line
Question when found is it posiible to trim
to 15 characters include spaces

I doubt whether this is all the code,
as this doesn't find the first word in a line only.
an example could be "the cat won the show"

I'd say, there are 5 words.

hmm...

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
S

Steved

Hello Helmut

The macro's objective is to highliight a word for example
Seasons Greetings it would highlight Seasons off Seasons Greetings

Now I would like if possible a trim Function that would give me Seasons
Greetin
which is 15 characters in length including spaces.

is this possible if so how please.

Thanks.
 
H

Helmut Weber

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"
 

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