PDA

View Full Version : Highlighting (Then Capitalizing) Specific Letters?



VB-AN-IZ
11-01-2015, 02:27 PM
One macro I use results in words at the start of some sentences being uncapitalized. (Long story, but it's ultimately a good thing.)

I can modify those uncapitalizations without applying a macro, using these steps, but I don't know how to replicate step four with a macro:

1) Ctrl + H to open 'Find and Replace', then select 'Find' tab
2) Check 'Use Wildcards'
3) Find '. [a-z]'
4) Find In > Main Document
5) Close 'Find and Replace'
6) Shift + F3

Assuming there's a way to highlight all the lower-case letters following a full stop and space, I'm pretty sure the next step involves something like:

Selection.FormattedText.Case = wdTitleWord

Sorry, bit of a novice; I'm mostly learning by recording keystrokes and analyzing afterwards, but by now I'm reduced to trial-and-error. Can't seem to get my brain around VBA's ranges.

Any help's much appreciated.

gmayor
11-02-2015, 06:21 AM
You'll find a useful primer on ranges on Greg Maxey's web site http://gregmaxey.mvps.org/word_tips.htm

In this case the following should work


Sub Macro1()
Dim oRng As Range
Dim iAsk As Long
Set oRng = ActiveDocument.Range 'the main document body
With oRng.Find
Do While .Execute(findtext:=". [a-z]", MatchWildcards:=True)
oRng.Select
iAsk = MsgBox(oRng.Text & vbCr & "Change the case of '" & oRng.Characters(3) & "' ?", vbYesNo)
If iAsk = vbYes Then
oRng.Characters(3).Case = wdUpperCase
End If
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

VB-AN-IZ
11-02-2015, 07:42 AM
Excellent, that works – it looks a lot more complicated than I thought it would be! Appreciate your help.

VB-AN-IZ
12-09-2015, 03:55 AM
In addition to this, how would I alter the macro so the changes are made automatically, without prompting the Yes/No question at each occurrence?

gmayor
12-09-2015, 07:14 AM
Remove the marked lines

Sub Macro1()
Dim oRng As Range
'Dim iAsk As Long
Set oRng = ActiveDocument.Range 'the main document body
With oRng.Find
Do While .Execute(findtext:=". [a-z]", MatchWildcards:=True)
oRng.Select
'iAsk = MsgBox(oRng.Text & vbCr & "Change the case of '" & oRng.Characters(3) & "' ?", vbYesNo)
'If iAsk = vbYes Then
oRng.Characters(3).Case = wdUpperCase
'End If
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

VB-AN-IZ
12-09-2015, 07:32 AM
Excellent. Thanks again!