PDA

View Full Version : [SOLVED:] Scenario based Capitalization VBA



Programmer_n
04-13-2016, 03:23 AM
Friends,

I am trying to create a capitalization VBA for different scenarios that my documents frequent.

The code below forms the basis of my approach.

With ActiveDocument.Range.Find
.ClearFormatting
With .Replacement.Font
.SmallCaps = False
.AllCaps = True
End With

.MatchWildcards = True
.Text = "\! ([a-z])"
.Replacement.Text = "! \1"
.Execute Replace:=wdReplaceAll
End With

Now the scenarios:



If a double quote succeeds a colon, capitalize the initial letter of word after the opening double quotes. ex. ( : "Capital blah blah blah")
If it is only double quotes, lowercase the initial letter after starting double quote ex. (sample words " lowercase.")
If a parentheses comes with in a sentence lowercase the first letter after parentheses ex. ( sample words (lowercase)
If a sentence starts with parentheses then capitalize initial letter after parentheses ex. Words. (Capital ....)


I am sure the answer is in .Text modification.:think:. I tired to use Char^34 ascii value for double quotes but not finding a proper solution.

Please help.

gmaxey
04-13-2016, 03:44 AM
There are several tough nuts to crack here. Chr(34) can serve as both an opening and closing quote. If you have a careless typist you could have a real mess on your hands.
Word does a very poor job of determining sentences.

Programmer_n
04-13-2016, 04:01 AM
gmaxey, I know the value of this statement: " Word does a very poor job of determining sentences" over past few days.

Additional query:

When I copy paste : " into find and replace; word is able to identify it and highlight. If we don't consider it as a sentence problem but as a character issue, in that case, is there any other way to tell word.

Look Word! whenever, you find : " capitalize the Initial letter after " and remember if you find ". don't do anything.

Btw, How about parentheses? it has ascii code 40 and 41.

Please help in this regard.

gmaxey
04-13-2016, 04:17 AM
You might try something like this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[" & Chr(34) & ChrW(8220) & "]"
.MatchWildcards = True
While .Execute
oRng.MoveStartWhile Chr(32) & Chr(9) & Chr(160), -1
If oRng.Characters.First.Previous.Text = ":" Then
oRng.Characters.Last.Next = UCase(oRng.Characters.Last.Next)
End If
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub

Programmer_n
04-13-2016, 04:30 AM
Thanks gmaxey.

By changing chr value to 40 and If oRng.Characters.First.Previous.Text = "." Then

a part of my parentheses issue is also solved.

gmaxey
04-13-2016, 04:40 AM
There you go! Glad to help.