Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Automatically Inserting Brackets.

Automatically Inserting Brackets

by Allen Wyatt
(last updated October 4, 2017)

11

Many different documents have many different requirements for their construction. For instance, you may have a document where it is necessary to put certain recurring words within brackets so that they stand out. (Some technical manuals use this convention to designate keystrokes, such as the [Enter] key.)

If you have a need such as this, you might find it helpful to simply type your text and then go back and later add the brackets. This is where a macro can come in handy to do the adding for you. For instance, you could select the word (double-click on it) and then run a macro that would add the brackets. The following macro will easily accomplish this task:

Sub AddBrackets()
 Dim iCount As Integer
 iCount = 1
 While Right(Selection.Text, 1) = " " Or _
 Right(Selection.Text, 1) = Chr(13)
 Selection.MoveLeft Unit:=wdCharacter, Count:=1, _
  Extend:=wdExtend
 iCount = iCount + 1
 Wend

 Selection.InsertAfter "]"
 Selection.InsertBefore "["
 Selection.MoveRight Unit:=wdCharacter, Count:=iCount
End Sub

The macro compensates for any spaces or paragraph marks at the end of your selection. When you are done running it, the insertion point is left at the end of the original selection. You can assign your macro to either a keyboard shortcut or to the Quick Access Toolbar.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12055) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Automatically Inserting Brackets.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Making VLOOKUP Trigger a Macro

VLOOKUP is an oft-used worksheet function to lookup values in a data table. If the function cannot return a value, it ...

Discover More

Evaluating Formulas

Need a bit of help in figuring out how Excel is evaluating a particular formula? It's easy to figure out if you use the ...

Discover More

Understanding File Paths

Every file on your disk drive has a unique file path that defines its location. Understanding how file paths work can be ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Understanding Functions

Do some macro programming in VBA and you'll quickly find out that you can use functions to extend the power and flexibility ...

Discover More

Getting User Input in a Dialog Box

Want to grab some interactive input from a user in your macro? The best way to do that is with the InputBox function, ...

Discover More

Highlighting Every Thousandth Character

Not satisfied with the detail provided by the Word Count feature in Word? Perhaps you want to actually know where every one ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 0?

2017-10-04 15:28:26

Derek Brown

I have a keyboard macro that, unless IP = 0, inserts "()" then moves back one character (and another one for "[]"). That way I don't have to worry about forgetting to insert the right-hand one -- I do have to remember to move right after typing the parenthesized/bracketed text, but of course watching the screen guarantees that I will.

To add parentheses/brackets to text that is already typed, if it is a single word, the cursor just needs to be somewhere in the word (IP = 1) when I run the macro, otherwise it will do the inserting if IP = 0 just as the sample macro does.


2017-10-04 11:16:51

Maggie

I usually have to add brackets to phrases, so I just deleted the first part of the macro and used only the Selection.InsertBefore and Selection.Insert.After, and it worked like a charm. Thanks!


2016-12-12 05:36:26

imran

What is shortcut key for inserting bracket () around word or sentence in MS Words.


2016-10-24 19:05:50

Dennis


great!!Now all I need to do is figure out what a macro is and how to use it.


2016-08-08 21:27:34

poy

how do you insert curly braces and vertical lines automatically.

from this: sentence 1.sentence 2.sentence 3.

to this: {sentence 1.|sentence 2.|sentence 3.}


2015-03-26 17:22:17

SJM

How would one modify this script to make the brackets a non-automatic, specific color - say blue? And to include a superscripted word to the left of the close bracket?


2014-07-24 13:31:18

Jeff

Oh, BTW note that I changed it to double brackets for my purposes only.


2014-07-24 13:26:04

Jeff

OK this seems to fix it:

Sub AddBrackets()
Dim selectionLength As Long
selectionLength = Selection.Characters.Count

Selection.MoveRight Unit:=wdCharacter, Count:=1 'cleared the selection and moved to the right end of it, now move to the beginning and select the whole thing again, from left to right this time!


Selection.MoveLeft Unit:=wdCharacter, Count:=selectionLength


Selection.MoveRight Unit:=wdCharacter, Count:=selectionLength, Extend:=wdExtend

Dim iCount As Integer
iCount = 1
While Right(Selection.Text, 1) = " " Or _
Right(Selection.Text, 1) = Chr(13)
Selection.MoveLeft Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
iCount = iCount + 1
Wend

Selection.InsertAfter "]]"
Selection.InsertBefore "[["
Selection.MoveRight Unit:=wdCharacter, Count:=iCount
End Sub


2014-07-24 11:59:00

Jeff

Update: I had removing and reinserting the module with the macro with the same result and no success, but it got working again when I renamed it first. Confirmed that selecting by dragging left indeed caused the problem - use this macro with caution. If you are using the brackets as an editorial indication of deletion (as I am), as opposed to document formatting, a trailing space in the brackets may not be a big deal and maybe you can get by with just the last three lines of code.


2014-07-24 11:52:16

Jeff

Running Word 2010, the Macro worked a couple of times, and then when I tried it on the word "a," it went crazy extending the selection to the left from the beginning of the selection instead of the end, all the way to the beginning of the document, causing Word to become unresponsive. I have no idea if I did something different in selecting the "a", like clicking and dragging left instead of double clicking, or if that would even matter. I tried it once or twice more with the same result. Now when I try to run it it does nothing at all.


2013-07-08 10:14:12

TM

Thank you for this! This will save me lots of time at work.

I need a slightly altered version of this code. I need the text inside the brackets and the brackets themselves to be bold. What would the modified code look like to reflect the bold text and brackets?


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.