PDA

View Full Version : changing capitals to Proper Case using word VBA



excel2006
02-24-2012, 05:02 AM
Hi

I have a docment where I want to find and replace all CAPITALISED
instances of the word "LECTURE" and put them in Proper case only if they are in a sentence.

e.g.

The LECTURE started at 5pm.

If they are a title i.e.

LECTURE

and it is the only word on the line, then I want it to remain CAPITALISED.

Thx

gmaxey
02-24-2012, 05:29 AM
Try:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "LECTURE"
.MatchCase = True
While .Execute
On Error GoTo Err_Handler
Select Case oRng.Characters.First.Previous
Case Chr(13), Chr(11)
Case Else
oRng.Case = wdLowerCase
Select Case oRng.Characters.First.Previous.Previous
Case " ", ". ", "! ", "? ", ": "
oRng.Characters.First.Case = wdUpperCase
Case Else
'
End Select
End Select
Err_ReEntry:
oRng.Collapse wdCollapseEnd
Wend
End With
Exit Sub
Err_Handler:
'LECTURE is first word in document. Most likely a title
Resume Err_ReEntry

End Sub

excel2006
02-24-2012, 05:54 AM
Thx for the reply, and I tried it. It works well in most instances, But the word remains capitalised if it's the first word of the sentence.
I need it to work so that whenever the word LECTURE appears in a sentence, it is set to Proper case.
It's only when the word LECTURE is completely on it's own then it reamins capitalised.

Thx

gmaxey
02-24-2012, 06:14 AM
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "LECTURE"
.MatchCase = True
While .Execute
On Error GoTo Err_Handler
Select Case oRng.Characters.First.Previous
Case Chr(13), Chr(11)
Select Case oRng.Characters.Last.Next
Case " ", ":"
oRng.Case = wdLowerCase
oRng.Characters.First.Case = wdUpperCase
End Select
Case Else
oRng.Case = wdLowerCase
Select Case oRng.Characters.First.Previous.Previous
Case " ", ". ", "! ", "? ", ": "
oRng.Characters.First.Case = wdUpperCase
Case Else
'
End Select
End Select
Err_ReEntry:
oRng.Collapse wdCollapseEnd
Wend
End With
Exit Sub
Err_Handler:
'LECTURE is first word in document. Most likely a title
Resume Err_ReEntry

End Sub

excel2006
02-27-2012, 04:12 AM
Hi Greg

I tired your seond macro but the same thing happened. The first word remains capitalised if it is part of the sentence, when it should be small.

I was thinking, would it be possible to say of the length of the line is <8, i.e. so that the coded knows that of the sentence is only 7 characters long then leave it as capitalised otherwise change it.

Thx for your help.

Frosty
02-27-2012, 02:07 PM
There's a little bit of a concept issue going on here. Based on your username, I'm assuming you are much more familiar with Excel concepts than Word concepts.

I think it would probably be better, rather than a macro (which after tweaking, will end up working, but still be limited to everything you've thought of within the macro, rather than all scenarios), to format your document properly. This would require
1. Find/Replace all instances of "lecture" (not matching case) with "Lecture"
2. Do a find on "^pLecture" (which should find each instance of "Lecture" immediately after a paragraph mark).
3. Move your cursor one character to the left, then one character to the right (so your cursor is in the *right* paragraph)
4. Apply a style (like, "Title" style, for example).
5. Format that style the way you want your headings to appear (i.e., make the Title style have a font format of All Caps).

You could record a macro for steps #2-#4, and then run that macro repeatedly until your document is formatted properly.

From then on, you can differentiate your Lecture "Titles" with the word Lecture within sentences or paragraphs.

If you are already familiar with Word styles, can you explain why they wouldn't work in this scenario? Using a macro in this case, apart from learning about Word VBA, seems a round-hole-solution to a square-peg-problem.

excel2006
02-29-2012, 06:09 AM
Thx Frosty.