Macro to toggle case?

C

Carrie Downes

I'm trying to create a basic editorial macro that will search for text in
small caps and replace it with regular text in "Title Case" (first letter of
each word selected is capitalized). Here's what I have so far, which appears
to clear the small caps but doesn't toggle the case properly:

Selection.Find.ClearFormatting
Selection.Find.Font.SmallCaps = True
Selection.Range.Case = wdNextCase
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.SmallCaps = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Is there a way to modify this so that I can specifically have it changed to
Title Case? Help is much appreciated! Thanks in advance.
 
C

Carrie Downes

Actually, is there a way to specify ANY particular case style as the
replacement text in the macro, whether it's lowercase, title case, sentence
case, etc.? Now that I'm looking at what I need to do, I think changing it to
lowercase would be more practical for my purposes. Any ideas?
 
J

Jay Freedman

Hi Carrie,

The problem you're running into is that there are two separate "case"
mechanisms in Word.

When you type a character into a document, the underlying character code can
be either lower case or upper case, depending on whether the Shift key is
down. Regardless of how the character is displayed, that code is what gets
saved in the document file.

Separately, there are font characteristics for Small Caps and All Caps. If
the underlying character code is lower case, then applying one of these
changes the way the character is displayed/printed -- but it's still
"really" lower case. (If the underlying character is upper case, applying
one of the formats has no effect.) Notice that there is *no* font formatting
that applies either title case or sentence case -- it just doesn't exist.

So if your macro finds and removes any Small Caps formatting from the text,
it will appear according to its underlying "real" nature. If you were seeing
small caps before the macro, you'll see lower case afterward.

The Format > Change Case command on the menu will actually change the
underlying character codes. This is where you'll find Title Case and
Sentence Case options. In a macro, the equivalent is the .Case property of
the Selection or Range object that points to the text. However, you can't
apply this directly to the .Replacement of a Find.

This code will find text in Small Caps, and then remove the Small Caps
formatting and change the case to lower case in two separate steps.

Public Sub SmCapToLowerCase()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Text = ""
.Font.SmallCaps = True
.Forward = True
.Wrap = wdFindStop

Do While .Execute
oRg.Font.SmallCaps = False
oRg.Case = wdLowerCase
oRg.Collapse wdCollapseEnd
Loop
End With
End Sub
 

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