How to detect whether selection/range is title case

G

Greg Maxey

Sub Test()
Dim i As Long
Dim pCase As String
i = Selection.Range.Case
Select Case i
Case 0
pCase = "lower case"
Case 1
pCase = "upper case"
Case 2
pCase = "title word"
Case 4
pCase = "title sentence"
End Select
MsgBox "The selected text is case """ & pCase & """"
End Sub
 
I

Ian B

Thanks Greg

I see the property only applies to a range object, which I had not looked
at.

Again thanks

Ian B
 
J

Jay Freedman

Hi

Is it possible to determine from a selection whether the selection is
"TitleCase", I can detect uppercase and smallcaps but not titlecase.
Graham Mayor has an excellent post at:
https://www.microsoft.com/communiti...4a8-9a78-c594ed28407e&lang=en&cr=us&sloc=&p=1

and I could probably adapt that to detect TitleCase but is there another
technique I can apply?

I suspect not!


TIA

Ian B

This isn't perfect, but may give you some ideas:

Private Function IsTitleCase(testRg As Range) As Boolean
Dim rtn As Boolean
Dim rgWd As Range
rtn = True
For Each rgWd In testRg.Words
If (Not rgWd.Characters(1) Like "[A-Z.,!?;:]") _
And (rgWd.Characters(1) <> vbCr) Then
rtn = False
Exit For
End If
Next

IsTitleCase = rtn
End Function

Public Sub test()
If Selection.Type = wdSelectionNormal Or Selection.Type = wdSelectionIP Then
If IsTitleCase(Selection.Sentences(1)) Then
MsgBox "Selection is title case"
Else
MsgBox "Selection is not title case"
End If
Else
MsgBox "Please select some text to test"
End If
End Sub

The idea is to test the first character of each word in the range passed as the
parameter testRg. If any word isn't capitalized, the range is deemed to be not
in title case. The difficulty is that the members of the collection testRg.Words
aren't necessarily what we consider to be "words". Each punctuation character
and paragraph mark is a separate member of the collection, so the If condition
has to take that into account.

As an aside, this test perpetuate's Microsoft's broken idea of what constitutes
title case. To the rest of the world, small words such as "the" or "and" aren't
capitalized unless they're the first word of the title. At least in Word 2007
the Change Case command finally calls what it does "Capitalize Each Word"
instead of "Title Case".
 
I

Ian B

Thanks for this Jay.

I applied Greg Maxey's solution which works just fine for my purposes.

Regards
Ian B


Jay Freedman said:
Hi

Is it possible to determine from a selection whether the selection is
"TitleCase", I can detect uppercase and smallcaps but not titlecase.
Graham Mayor has an excellent post at:
https://www.microsoft.com/communiti...4a8-9a78-c594ed28407e&lang=en&cr=us&sloc=&p=1

and I could probably adapt that to detect TitleCase but is there another
technique I can apply?

I suspect not!


TIA

Ian B

This isn't perfect, but may give you some ideas:

Private Function IsTitleCase(testRg As Range) As Boolean
Dim rtn As Boolean
Dim rgWd As Range
rtn = True
For Each rgWd In testRg.Words
If (Not rgWd.Characters(1) Like "[A-Z.,!?;:]") _
And (rgWd.Characters(1) <> vbCr) Then
rtn = False
Exit For
End If
Next

IsTitleCase = rtn
End Function

Public Sub test()
If Selection.Type = wdSelectionNormal Or Selection.Type = wdSelectionIP
Then
If IsTitleCase(Selection.Sentences(1)) Then
MsgBox "Selection is title case"
Else
MsgBox "Selection is not title case"
End If
Else
MsgBox "Please select some text to test"
End If
End Sub

The idea is to test the first character of each word in the range passed
as the
parameter testRg. If any word isn't capitalized, the range is deemed to be
not
in title case. The difficulty is that the members of the collection
testRg.Words
aren't necessarily what we consider to be "words". Each punctuation
character
and paragraph mark is a separate member of the collection, so the If
condition
has to take that into account.

As an aside, this test perpetuate's Microsoft's broken idea of what
constitutes
title case. To the rest of the world, small words such as "the" or "and"
aren't
capitalized unless they're the first word of the title. At least in Word
2007
the Change Case command finally calls what it does "Capitalize Each Word"
instead of "Title Case".

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
 

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