PDA

View Full Version : Solved: Need a Macro to change Casing



Rakesh
08-31-2010, 12:49 AM
Hi Guys,

I had a word Document which contains several styles. I need a macro to change ALL Caps to Initial Case (Whole para) which contains “SOI_P_H2” Style. Can it possible to do it?

Thanks
Rakesh

fumei
08-31-2010, 10:20 AM
Say what????

"I had a word Document which contains several styles. I need a macro to change ALL Caps to Initial Case (Whole para) which contains “SOI_P_H2” Style. Can it possible to do it?"

Very likely, but I am not quite following what you want to do.

You have a style named SOI_P_H2. Yes? And you want to change all paragraphs using that style to Initial Case?

Yes? If so, then just change the style itself. That is the whole point of using styles.

Rakesh
08-31-2010, 08:57 PM
Hi Fumei,

Thank you for your kind reply and update.

If there is only one occurrence I will change it manually. But I have multiple occurrence.

So what to do in that case. For that only I need a macro.

Thanks
Rakesh

geekgirlau
08-31-2010, 09:55 PM
Fumei is pointing out that if you had set the style up properly, you wouldn't actually have to type the text in uppercase as the font format would do that for you. Then fixing every sentence is simply a matter of editing the style once.

If (as I suspect) the sentences are typed in uppercase, try the following:


Sub FixCase()
Dim para As Paragraph


For Each para In ActiveDocument.Paragraphs
If para.Style = "SOI_P_H2" Then
para.Range.Case = wdTitleSentence
End If
Next para
End Sub

Rakesh
08-31-2010, 10:13 PM
Hi geekgirlau,


Its working fine. Thanks a ton.


Thanks,
Takesh
:mbounce2:

hitech
09-01-2010, 04:09 AM
Hey

What will happen with this code? What will change?

fumei
09-01-2010, 09:58 AM
"What will happen with this code? What will change?"

What happens is: every paragraph using the SOI_P_H2 style will be set as TitleSentence case.

I will repeat it though. It is far, far, far better to use styles properly.

geekgirlau
09-01-2010, 03:33 PM
I don't know whether you have found the same Gerry, but I find that in places that have All Caps in the style, people tend to get very lazy about typing, usually entering text completely in lowercase. Defeats the purpose if you change your mind about the format later!

fumei
09-02-2010, 12:08 PM
I am not following. If the style itself is All Caps, if you type text in lower case...it becomes All Caps as you type. In fact, it is not possible to type lower case text. All text types matches the style. If the style is All Caps, you can type lower case, or any combination you like - the text becomes All Caps.

geekgirlau
09-08-2010, 03:54 PM
The issue comes if you later decide that you don't want All Caps. If your style has All Caps and you have typed the text in lower case, it looks fine. However if you remove All Caps from the style, you need to physically change the case of your text - there's no corresponding setting you can add to the style to make it look like proper case for example.

fumei
09-09-2010, 10:33 AM
If NameStyle is setr as All Caps, and you type lower case it becomes ALL CAPS.

If you then change the style itself to NOT being All Caps, the text typed in will revert to lower case.

Will it go to Proper Case? No, you are correct, but then...you can not do that with any style in the first place! Proper Case (if what you mean is Title Case) is not, not has it ever been, an attribute of a style.

In this you are correct, the ONLY way to change every sentence (of ANY style) to a specific Case, is through VBA.

geekgirlau
09-09-2010, 06:00 PM
Interestingly there is a PROPER function in Excel, that does exactly that. Given you can change the appearance to uppercase in a style in Word, why not be able to force every other case variation?

fumei
09-10-2010, 10:56 AM
Case is not a property of Font.

Case is a ENUM of wdCharacterCase which in turn is a property of RANGE.

Thus making the following possible.

You have a Style that is NOT all caps. You select a chunk, and use Format > Change Case. You make the selected text ALL CAPS.

It works. Yes?

The all caps is applied to the range of the Selection. It can of course be applied to any defined range. A style is not applied to a pre-defined range. A range can be defined, and THEN a style applied to it.

Just like in the example above, if after you have changed the Case (against the selected range), you explicitly applied the Style (NOT all caps) to the same selected text. It will NOT - repeat NOT - change the Case to what is in the Style. Why...because the Case is not IN the style. Style has no comment about Case.

When you put All Caps into a style, you are using the .Font property, not the range property.

geekgirlau
09-10-2010, 09:23 PM
Understood Gerry, but my comment is actually about the font property. If there is a font property to give text the appearance of uppercase, then why not a font property for all other variations of case?

fumei
09-13-2010, 02:57 PM
Hi Anne. Why? Because Case is not a Font property.

Yes, yes, I know Excel does things differently, but that is because Excel is not a true word-processor. It is a number-based application so number-based people can pretend they are using a word-processor.

Just kidding. Shrug. What can I say? It may seem better, but within the structure of the Document Model (and the Object Model), Word is, IMO, acting like a proper word-processor.

TitleCase is NOT a property of Font. And, IMO, correctly so.

" If there is a font property to give text the appearance of uppercase, then why not a font property for all other variations of case?"

Except...ummmmm...there IS font property to give the appearance of uppercase, true. And what happens when you apply it? ALL relevant text becomes uppercase. But, TitleCase is selectively applied - wait for it - across the range. SOME text is uppercase, and some is not. To do this means a processing of - wait for it - words. And what child object are Words? Why Range of course.

TitleCase is NOT a property of Font. It is an ENUM of Case, a property of Range...not Font.