PDA

View Full Version : [SOLVED:] Macro in Word to Combine Text and Numbers



rsrasc
01-29-2022, 12:13 PM
Hi all,

Hope you all doing well.

I need some little help here.

I found the following code in the internet and seems to be partially working for what I need.

I was wondering if the following code can be modified so it can bold the word "Chapter" with any number (could be any number from 1 up to 150) in the document.




Thank you for your support.




Sub Formatting_Chapter()
Dim A(3) As String
A(1) = "Chapter 1"
A(2) = "Chapter 2"
A(3) = "Chapter 3"
For i = 1 To 3
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Bold = True
.Replacement.Font.ColorIndex = wdRed
.Execute FindText:=A(i), ReplaceWith:=A(i), Format:=True, _
Replace:=wdReplaceAll
End With
Next i
End Sub

macropod
01-29-2022, 04:29 PM
You don't need VBA for this - it can all be done with a single wildcard Find/Replace, where:
Find = Chapter [0-9]@>
Replace = \1
and you set the replacement font colour appropriately.

rsrasc
01-31-2022, 05:30 AM
Hi Macropod, thank you for the code. Is working great! Much appreciated!