PDA

View Full Version : Modifying Title Case



HJ Norman
12-19-2011, 08:23 PM
There just is no effect on execution of the following code. Wonder where lies the error!?
Sub ModTCase()
Dim aWord As Word.Range
For Each aWord In Selection.Range.Words
Select Case aWord
Case " In ", " On "
aWord.Case = wdLowerCase
End Select
Next
End Sub
Thanks.

gmaxey
12-20-2011, 06:43 AM
I would start by removing the space before the I and O.

HJ Norman
12-20-2011, 08:15 AM
I would start by removing the space before the I and O.
Thanks for the response.
Two things here:
1. Removing the spaces as suggested doesn't make any difference in the result.
2. Removing the spaces defeats the purpose, in fact. The intention is to make the "In" and "On" lowercase in the middle of sentences. Without the preceding space it lowers the case of the starting Ins and Ons, which is not desirable.

gmaxey
12-20-2011, 08:42 AM
Sorry about that. I couldn't find this code earlier and so I winged it.

Sub MyTitleCase()
Dim oRng As Range
Dim oRng2 As Range
Dim arrFind As Variant
Dim arrReplace As Variant
Dim i As Long
Dim m As Long
Set oRng = Selection.Range
If Selection.Type = wdSelectionIP Then
MsgBox "Select the text you want to process.", vbOKOnly, "Nothing Selected"
Exit Sub
End If
'Format the selected text using title case.
oRng.Case = wdTitleWord
'Create exceptions
arrFind = Split("A,An,And,As,At,But,By,For,If,In,Of,On,Or,The,To,With", ",")
'Create replacements
arrReplace = Split("a,an,and,as,at,but,by,for,if,in,of,on,or,the,to,with", ",")
With oRng
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindStop
.MatchWholeWord = True
.MatchCase = True
For i = LBound(arrFind) To UBound(arrFind)
.Text = arrFind(i)
.Replacement.Text = arrReplace(i)
.Execute Replace:=wdReplaceAll
Next i
End With
'Ensure first word is a capitalized
oRng.Characters.First.Case = wdUpperCase
'See if range contains a colon
If InStr(1, oRng, ":") > 0 Then
m = InStr(1, oRng, ":")
Set oRng2 = oRng
oRng2.Start = oRng.Start + m
oRng2.MoveStartWhile Cset:=" ", Count:=wdForward
'Cap word following colon.
oRng2.Characters.First.Case = wdUpperCase
End If
End With
End Sub

HJ Norman
12-20-2011, 09:30 AM
Thanks so much.
I'm giving a touch up job to some library book titles, and your code serves the purpose, as they are only one liners. So, for now, I'll make full use of your code.
However, just one observation. Sentences starting with the array words (A,An,And,As,At...) are also lowered down in case.
I still am trying to understand why the code I'd shown fails so!

gmaxey
12-20-2011, 09:38 AM
Your code did what is is coded to do. oWord will never = " On " because words in work don't start with spaces:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oWord As Range
For Each oWord In Selection.Words
Debug.Print Asc(oWord.Characters.First) & " "; Asc(oWord.Characters.Last)
Next oWord
End Sub

gmaxey
12-20-2011, 05:15 PM
Your orignal code would need to look something like this:

Sub ModTCase()
Dim aWord As Word.Range
For Each aWord In Selection.Range.Words
Select Case aWord
Case "In ", "On ", "In", "On"
aWord.Case = wdLowerCase
End Select
Next
Selection.Range.Words(1).Characters(1).Case = wdUpperCase
End Sub


to account for the word On or In occurring at the start or end of the selection.

macropod
12-20-2011, 05:31 PM
to ensure each sentence begins with a capital letter, you could replace:
'Ensure first word is a capitalized
oRng.Characters.First.Case = wdUpperCase
with something like:

'Ensure first character of each sentence is capitalized
For Each Sntnc In oRng.Sentences
Sntnc.Range.Characters.First.Case = wdUpperCase
Next