An implementation of Visual Basic that is built into Microsoft products.
Use one macro that keeps track of how many times it has been run in the current session and calls a common routine with the correct number. For example, put this in a standard module:
Option Explicit
Dim gRunCount As Long
Sub OneButtonMacro()
'Increment run counter each time the macro is pressed
gRunCount = gRunCount + 1
Select Case gRunCount
Case 1
ProcessSelection 1
Case 2
ProcessSelection 2
Case 3
ProcessSelection 3
Case Else
'After third time, stay at 3 or reset as desired
gRunCount = 3
ProcessSelection 3
End Select
End Sub
Private Sub ProcessSelection(ByVal n As Long)
Selection.Find.ClearFormatting
With Selection.Find
.Text = "|"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=CStr(n)
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Color = wdColorRed
Selection.Font.Bold = True
Selection.Range.HighlightColorIndex = wdYellow
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.ParagraphFormat.TabStops.Add _
Position:=CentimetersToPoints(18.75), _
Alignment:=wdAlignTabLeft, _
Leader:=wdTabLeaderSpaces
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:="$"
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Bold = True
Selection.Font.Color = wdColorBlue
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1
End Sub
Usage:
- Assign
OneButtonMacroto a button or keyboard shortcut (for example via the Record Macro dialog and Quick Access Toolbar customization). - Place the cursor for the first location and run
OneButtonMacro→ it inserts and formats1. - Move the cursor to the second location and run
OneButtonMacroagain → it inserts and formats2. - Move the cursor to the third location and run
OneButtonMacroagain → it inserts and formats3.
The shared ProcessSelection routine contains the logic from the three original macros; the only difference is the number inserted, passed as n.
References:
- Working with the Selection Object
- Selection object (Word)
- Use a screen reader to create a macro in Word
- Keyboard shortcuts in Word
- Word Remove Color and highlight - Microsoft Q&A
- Block Text - Microsoft Q&A
- Editing / removing Paragraphs - Microsoft Q&A
- How to create a shortcut key for "Accept change" and advance to the next change - Microsoft Q&A
- How do I select text with a trackpad? - Microsoft Q&A
- Sample macro that uses a loop to find text in Word documents