I'm trying to write a macro to change the case of "section 1", "section 2", etc. to title case, so they all read "Section 1" etc. I've adapted Variatus' very helpful code here:
Dim Rng As Range
Dim Fnd As Boolean
Set Rng = Selection.Range
With Rng.Find
.ClearFormatting
.Execute FindText:="section [0-9]", Forward:=True, _
Format:=False, Wrap:=wdFindContinue, MatchWildcards:=True
Fnd = .Found
End With
If Fnd = True Then
Rng.Case = wdNextCase
End If
The issue I'm having with this is that it only changes one instance at a time. Ideally it would change the case of all instances with one keypress.
As a bonus, I'd also like it to register the change of case as a tracked change. As I'm using tracked changes, I can't do a simple replace text macro with section ([0-9])
for Section \1
, as the bug with tracked makes it "1Section ", "2Section ", etc. This isn't essential but would be a really nice bonus. The .Case
function doesn't get tracked, and .Font
only has an option for .AllCaps
.
Perhaps there's a way it can find section [0-9]
, move the cursor to the beginning of the word and select the first letter, set .Font.AllCaps = True
, and loop until there are no more instances of section [0-9]
? Just an idea, but that's way beyond my macro ability at the moment. The main thing for now is getting the above code to apply to all instances of section [0-9]
.
Cheers!
Try:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "section [0-9]"
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While .Find.Execute
.Characters.First.Text = "S"
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.