Log in

View Full Version : [SOLVED:] VBA Newbie Trying to Find/Replace All Caps after Tab



devlon
10-09-2022, 01:04 PM
Hi, everyone.

I'm trying to create a macro in Word that will quickly find/replace instances in documents where certain characters followed by a tab are always followed by a capital letter.

For instance:

Q (tab) hello
A (tab) goodbye

Would always be changed to

Q (tab) Hello
A (tab) Goodbye

I tried (silly me) using the Find/Replace tab designator of ^t in a trial macro, but then realized I probably needed to use vbtab in some fashion, but can't seem to get the syntax right.

So far I've gotten


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


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

But that is not working at all, haha. I'm not sure how to only select Q and A as the character preceding a tab, and how to continue the syntax to include any lowercase character after the tab.

macropod
10-10-2022, 02:13 PM
You could use a single wildcard Find/Replace, where:
Find = [QA]^t[a-z]
Replace = ^&
and you set the replacement font characteristic to All Caps.

There is no need for a macro. That said, as a macro:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[QA]^t[a-z]"
.Replacement.Text = "^&"
.Replacement.Font.SmallCaps = False
.Replacement.Font.AllCaps = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub