PDA

View Full Version : Find Lower Case at Start of Each Paragraph and Capitalize?



VB-AN-IZ
01-25-2018, 03:28 PM
How would I find any lower-case letters at the start of a paragraph and capitalise them?

For example, turning this:

where it began, I can't begin to knowing


but then I know it's growing strong
was in the spring
then spring became the summer
who'd have believed you'd come along


...into this:

Where it began, I can't begin to knowing
But then I know it's growing strong
Was in the spring
Then spring became the summer


Who'd have believed you'd come along

Without a macro, you can find ^13[a-z] (wildcards on) and select Find In > Main Document, then select UPPERCASE from the Font > Change Case area. Recording that created this macro, which is not replicable:


Selection.Find.ClearFormatting
With Selection.Find
.Text = "^13[a-z]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Range.Case = wdUpperCase
End Sub

I suspect the solution begins with this but then I get lost...


Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(findtext:="^13[a-z]", MatchWildcards:=True)

Thanks for any help!

gmayor
01-25-2018, 09:52 PM
The following should do the trick. The reason that the first words of the paragraphs may not have been capitalised, is that lyrics and poetry are usually created using line breaks rather than paragraph breaks, so you need to get rid of those first e.g.


Sub Macro1()
Dim oRng As Range
Dim oPara As Paragraph
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(Chr(11))
oRng.Text = vbCr
oRng.Collapse 0
Loop
End With
For Each oPara In ActiveDocument.Paragraphs
oPara.Range.Characters(1).Case = wdUpperCase
Next oPara
lbl_Exit:
Set oRng = Nothing
Set oPara = Nothing
Exit Sub
End Sub

VB-AN-IZ
01-27-2018, 12:34 AM
Thank you! I did not foresee it having to be so specific. I imagine that being able to change the case of existing characters could be useful in numerous situations, rather than just at the start of each paragraph – is there a more versatile way to find a lower-case letter before/after specific characters, and change its capitalization? For example, finding mc[a-z] or Mc[a-z] and just capitalizing the letter within the square brackets? Or would you have to set up a find/replace for each letter of the alphabet in each of those scenarios?

gmayor
01-27-2018, 01:26 AM
That's a different question, however the principles are similar. You identify the string you want to process and process it e.g. for your specific example Mc or mc followed by a letter in the range a-z
The more vague your requirement, the more there are opportunities for misidentification. You need to tie it down as tightly as possible, to avoid missed incidences or unwanted changes. VBA doesn't deal in vague.

Sub Macro1()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="<[Mm]{1,}c[a-z]{1,}>", MatchWildcards:=True)
oRng.Characters.Last.Case = wdUpperCase
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub


For other text strings see http://www.gmayor.com/replace_using_wildcards.htm