Define paragraph range

A

Angie M.

Hello,

I need to create a macro that inserts a bracket at the start and end of a
portion of text. The user will select the text (it can be a few characters
or an entire page). Any help would be appreciated. I'm on Word 03.

Thank you
 
G

Graham Mayor

How about

Sub AddBrackets()
Dim sText As String
sText = Selection
Selection.TypeText "(" & sText & ")"
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
K

Klaus Linke

Or a more elaborate version:

Sub AddBrackets()
Dim myRange As Range
Dim myFont As Font

' If nothing is selected, select current word
If Selection.Type = wdSelectionIP Then
Selection.Words(1).Select
End If
Set myRange = Selection.Range.Duplicate

' remove trailing space if necessary
myRange.MoveEndWhile Cset:=" ", Count:=wdBackward

' braces should get all the font properties of the first character
Set myFont = myRange.Characters(1).Font.Duplicate

myRange.InsertBefore "("
myRange.InsertAfter ")"
myRange.Characters.First.Font = myFont
myRange.Characters.Last.Font = myFont
myRange.Select

End Sub

-- select current word if nothing is selected,
-- does not loose any formatting in the selected text,
-- make sure the opening brace has the formatting of the first character,
-- make sure the closing brace has the same formatting as the opening brace,
-- remove trailing spaces (from Word's "smart word selection")

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top