PDA

View Full Version : [SOLVED:] Change certain words to UPPER case.



PAB
10-10-2019, 02:38 PM
Good evening,

In WORD 2007.

I have a list of certain words [about 20 words] that I would like to change to UPPERCASE using VBA.
Is there any way that I can do this please using something like two arrays. One for the way the words are currently, and the other for the way that I want the words changed to please?

Thanks in advance.

gmaxey
10-10-2019, 03:38 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim arrFind() As String
Dim lngIndex As Long
arrFind = Split("now|is|the|time|for|all|good|men", "|")
For lngIndex = 0 To UBound(arrFind)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = arrFind(lngIndex)
.Replacement.Text = UCase(arrFind(lngIndex))
.Execute Replace:=wdReplaceAll
End With
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

PAB
10-10-2019, 04:59 PM
That's brilliant Greg, thank you so much, it is appreciated!

If I wanted to change them back to LOWERCASE, I thought that changing the UCase to . . .


.Replacement.Text = LCase(arrFind(lngIndex))

. . . would work but it doesn't!

Thanks in advance.

gmayor
10-11-2019, 02:23 AM
The following should work

arrFind = Split(UCase("now|is|the|time|for|all|good|men"), "|") For lngIndex = 0 To UBound(arrFind)
Set oRng = ActiveDocument.Range
With oRng.Find
.MatchCase = True
.ClearFormatting
.Text = arrFind(lngIndex)
.Replacement.Text = LCase(arrFind(lngIndex))
.Execute Replace:=wdReplaceAll
End With
Next lngIndex

PAB
10-11-2019, 03:15 AM
That's brilliant Graham, thank you so much, it is appreciated!

I can just go through the few that change the words like copying [after using UPPERCASE] to COPYINg copy and in in the array].

Have a great weekend Greg and Graham and thank you both again, it is appreciated!

gmayor
10-11-2019, 04:39 AM
Add
.MatchWholeWords = True
and that should overcome the part word issue.

PAB
10-11-2019, 05:10 AM
That's brilliant Graham, thank you so much, it is appreciated!

I just had to change . . .
.MatchWholeWords = True to . . .
.MatchWholeWord = True and it works perfectly!