Tech Support Forum banner
Status
Not open for further replies.
1 - 3 of 3 Posts

·
Registered
Joined
·
3 Posts
Discussion Starter · #1 ·
In the "good old" days, macros just recorded your keystrokes. Not so any more.

I need to help with a macro that will work on selected text. I want to select the text. Then, when I run the macro, a left bracket will be put at the beginning of the text I selected, a right bracket will be put at the end of the text I selected, and all (including the brackets) will be highlighted in a particular color, with the cursor ending up at the right of the closing bracket.

I vaguely imaging some version of with. and end with needs to be used but that's about as much as I know.
 

·
Moderator , Microsoft Support, MS Office Pro
Joined
·
2,540 Posts
Hi Jeff,

What you're asking is really rather basic, and could be gleaned from a simple web search or any half-decent book on macros. Anyway, FWIW:

Code:
Sub Demo()
Selection.InsertBefore "("
Selection.InsertAfter ")"
Selection.Font.Color = wdColorAqua
Selection.Collapse wdCollapseEnd
End Sub
or
Code:
Sub Demo()
With Selection
  .InsertBefore "("
  .InsertAfter ")"
  .Font.Color = wdColorAqua
  .Collapse wdCollapseEnd
End With
End Sub
 

·
Registered
Joined
·
3 Posts
Discussion Starter · #3 · (Edited)
I do realize that what I was asking was basic - searching the 'net brought me to the great folks here. In any case, it turns out that what I needed was either of these (apply highlight color rather than font color). Note, the added spaces between the right bracket and the closing quote should not be there. The BBS software here is inserting them for some reason.

Code:
Sub HighlightSelectedText()
   Selection.InsertBefore "["
   Selection.InsertAfter "]"
   Selection.Range.HighlightColorIndex = wdTurquoise
   Selection.Collapse wdCollapseEnd
End Sub
'
'or
'
Sub HighlightSelectedText()
   With Selection
      .InsertBefore "["
      .InsertAfter "]"
      .Range.HighlightColorIndex = wdTurquoise
      .Collapse wdCollapseEnd
   End With
End Sub
 
1 - 3 of 3 Posts
Status
Not open for further replies.
Top