PDA

View Full Version : Upper and lower cases in a title



r591
05-06-2010, 01:06 AM
Hi, all!

It is enjoyble to learn WORD macros and seek help here.

I have a question as follows.

The title of an article is like this:

study on management mechanism in construction of energy saving monitor systems

By using:
Selection.Range.Case = wdTitleWord

I can get:
Study On Management Mechanism In Construction Of Energy Saving Monitor Systems

But, I don't want "on", "in" or "of" be capitalized.

That is, I'd like to have a macro to capitalize the first letter of each word in a selection, but leave the specified words in lowercase.

If I remember it correctly, "IF THEN" in APPLE BASIC can work with something like "DATA: "in", "on", "of"......" to specify such words to be in lowercase. It is quite convenient. Is there any similar way in VBA? If not, what is the easiest?

A complete macro is highly appreciated!

macropod
05-06-2010, 03:05 AM
Hi r591

Perhaps the simplest approach is to use the existing code, then use a Find/Replace loop that calls an array of words to re-set to lowercase the words you don't want to capitalise.

r591
05-06-2010, 06:57 PM
Thank you, macropod!
I'll try it.

fumei
05-07-2010, 09:16 AM
Kind of like this (added "The" as it is a common one).
Sub KindOfTitleCase()
Dim aWord
For Each aWord In Selection.Range.Words
Select Case aWord
Case "On ", "In ", "Of ", "The "
aWord.Case = wdLowerCase
End Select
Next
Selection.Collapse 1
End Sub

TonyJollans
05-07-2010, 09:36 AM
Personally I would leave the first word capitalised, whatever it was, so a slight adjustment to the code:

Sub KindOfTitleCase()
Dim aWord
For Each aWord In Selection.Range.Words
Select Case aWord
Case "On ", "In ", "Of ", "The "
aWord.Case = wdLowerCase
End Select
Next
Selection.Range.Words(1).Case = wdTitleWord
Selection.Collapse 1
End Sub

r591
05-08-2010, 05:19 AM
Very good!
Thank you all for showing the "beauty in programming"!

fumei
05-10-2010, 10:06 AM
"Personally I would leave the first word capitalised, whatever it was"

Indeed. Nice catch.