String variable question: if more than 2 words, how to reduce to first 2?

E

Edward Mendelson

One more question about string manipulations, if anyone still has the
patience to deal with these.

Thanks to Helmut, Jay, and Greg, I can now extract a font name from a symbol
field, with results(in the macro for which this will be useful) that look
like this:


WP MultinationalA Roman
WP MultinationalB Courier
WP TypographicSymbols (no space after Typographic)
Multinational Ext
Typographic Ext
etc. etc

Some of these font names consist of three words; some have only two. (It's
possible that some will have only one word, but I haven't encountered any
yet.)

I want to be able to take any font name that has three words and reduce the
name to the first two words only, so that the first and second items in the
list above will *both* return:

WP MultinationalA

I've been struggling with the functions found here:

http://www.romankoch.ch/capslock/strfun.htm

but haven't had any luck. Any help gratefully received.

Edward Mendelson
 
G

Greg Maxey

Edward,

I don't think a marcor is really required here. You can do this with
Edit>Replace.

You are looking for stings of three words and a line break and you want to
replace with the first two words and the line break.

Edit>Replace>More Use Wildcards.
Type in Find: (<*>) (<*>) (<*>)^l
Type in Replace: \1 \2^l
Replace all.

Here is a macro record of the process.


Sub Macro1()
'
' Macro1 Macro
' Macro recorded August 22, 2004 by Gregory K. Maxey
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(<*>) (<*>) (<*>)^l"
.Replacement.Text = "\1 \2^l"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
E

Edward Mendelson

Hi Greg,

Actually, I was looking for help for a question where find/replace doesn't
apply, I think, because I'm manipulating a string returned in the course of
running a macro.

Here are details: in the thread called "How to extract strings from the code
inside a field," Helmut and you and Jay Freedman provided a lot of help with
the answer. The string I extract (thanks to your code) is either two or
three words long. My question is: how to take this string variable, and, if
the variable is longer than two words, reduce it to the first two only?

One alternative way of approaching the question is this:

The only three-word strings that I'm likely to see will be named

WP MultinationalA Courier
WP MultinationalA Helve
WP MultinationalA Roman
WP MultinationalB Courier
WP MultinationalB Helve
WP MultinationalB Roman
WP Greek Century
WP Greek Courier
WP Greek Helve

In each case, is there a simple test that would let me reduce any of the
first three of these to "WP MultinationalA", any of the second three to "WP
MultinationalB", any of the third three to "WP Greek"?

Thanks again!

Edward Mendelson


----- Original Message -----
From: "Greg Maxey" <[email protected]>
Newsgroups: microsoft.public.word.vba.general
Sent: Sunday, August 22, 2004 10:25 PM
Subject: Re: String variable question: if more than 2 words, how to reduce
to first 2?
 
J

Jezebel

On way to approach this is to use the split() function:

Dim pWords() as string
pWords = split(FontName)

pWords() now contains the individual words of the name. Ubound(pWords) tells
you how many words there are, minus one because the array starts with zero.

So you could then use --

if ubound(pWords) = 0 then 'Just one word
pFontName = pWords(0)
else 'All other cases, use
the first two words
pFontName = pWords(0) & " " & pWords(1)
end if
 

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