Assigning selected text to autocorrect, changing case

A

Alan Stancliff

I learned that it is possible to write a macro to assign selected text to an
autocorrect entry from another thread in this forum. So if autocorrect entry
is "uu", then the code would be:
AutoCorrect.Entries.Add Name:="uu", Value:=Selection.Text

But suppose the selected text is in upper case and one wants it to be
assigned to "UU" in title case. How would one do that?
 
J

Jay Freedman

I learned that it is possible to write a macro to assign selected text to an
autocorrect entry from another thread in this forum. So if autocorrect entry
is "uu", then the code would be:
AutoCorrect.Entries.Add Name:="uu", Value:=Selection.Text

But suppose the selected text is in upper case and one wants it to be
assigned to "UU" in title case. How would one do that?

Use the StrConv function with the vbProperCase argument to change the string
before storing it:

Sub demo()
Dim s As String
If Selection.Type = wdSelectionNormal Then
s = StrConv(Selection.Text, vbProperCase)
AutoCorrect.Entries.Add Name:="UU", Value:=s
End If
End Sub
 
A

Alan Stancliff

Thanks Jay,

As you can probably tell, I'm a beginner at this. Your advice and website
are both very informative and interesting.

Regards,

Alan
 
A

Alan Stancliff

Hi Jay,

I hope you see this. It appears that the suggested code:

s = StrConv(Selection.Text, vbProperCase)
AutoCorrect.Entries.Add Name:="UU", Value:=s

Does not convert to title case but to upper case.

This macro is supposed to copy a selected name in upper case in a
demographic sheet and change it to title case. Instead, it seems to change it
to sentence case. That's OK if the name on the demographic is JOHNSON and the
autocorrect entry for UU is Johnson. But it falls apart when it is a compound
name like FERNANDEZ-GUTIERREZ. Then the code assigns UU the value of
Fernandez-gutierrez.

I have been looking around for a way to fix this, as I have to deal with a
lot of compound names.

Regards,

Alan
 
G

Graham Mayor

I don't think it would ever be possible to fix a hyphenated name so simply,
however

Dim sText As String
sText = Selection.Text
If InStr(1, sText, "-") Then
sText = Replace(sText, "-", " ")
sText = StrConv(sText, vbProperCase)
sText = Replace(sText, " ", "-")
Else
sText = StrConv(sText, vbProperCase)
End If
AutoCorrect.Entries.Add Name:="UU", Value:=sText

will do so.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

Alan Stancliff

Thanks Graham,

That definitely did it. Now I've got to get me some books on VBA as I'm
beginning to be "curiouser and curiouser."

Regards,

Alan
 
G

Graham Mayor

You'll probably learn more from the Word vba help and by regularly following
the practical solutions in this forum.
http://www.gmayor.com/MSNews.htm may help make this forum -
microsoft.public.word.vba.general - more accessible.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

Alan Stancliff

Thanks for the link, Graham. I'll try to set that up on my "Agent"
newsreader. If that fails, I'll fire up OE.

Regards,

Alan
 

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