Inserting a Formatted Text Box with a Macro

Written by Allen Wyatt (last updated February 4, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


7

Ross is trying to write a macro to put a text box around selected text (that part works) and then to format the box with a transparent background, transparent border, and floating in front of text. Unfortunately, the macro recorder won't record any but the insertion of the text box, and Ross can't work out command to do the other steps. He wonders how to make the macro work as he would like.

The Macro Recorder is handy for getting started with macros in general or for use on short individual macros. There are many, many things that cannot be done with the Macro Recorder, and it is necessary to take your initially recorded macro, pull it up in the VB Editor, and make changes to it so it will do what you want.

If I follow the steps that Ross outlines in his query (select some text, turn on the Macro Recorder, insert the text box, format it as described), this is what it creates:

Sub MyBox
'
' MyBox Macro
'
'
    Selection.CreateTextbox
End Sub

Very simple—a line that creates a text box around the selection. None of the subsequent formatting steps I took were recorded, so this is where the need for manual coding in the VB Editor comes into play. In order to have the macro do the formatting, all you need to do is add the proper VBA commands, after the creation of the text box, to make it happen:

Sub MyBox()
'
' MyBox Macro
'
'
    Selection.CreateTextbox
    Selection.ShapeRange(1).WrapFormat.Type = wdWrapFront
    Selection.ShapeRange(1).Line.Transparency = 1
    Selection.ShapeRange(1).Fill.Transparency = 1
    Selection.Font.ColorIndex = wdBlack
End Sub

Provided you select some text first, this macro will create a text box that contains that text and then format it as Ross wanted.

If you don't select text first, then you'll get an error when you run the macro. You can, if desired, enhance the macro so that it checks to see if a selection has been made before it does its magic. The following does that, as well as condensing the code just a bit:

Sub MyBox()
'
' MyBox Macro
'
'
    If Selection.Type <> wdSelectionIP Then
        With Selection
            .CreateTextbox
            .ShapeRange(1).WrapFormat.Type = wdWrapFront
            .ShapeRange(1).Line.Transparency = 1
            .ShapeRange(1).Fill.Transparency = 1
            .Font.ColorIndex = wdBlack
        End With
    Else
        MsgBox "No text selected!"
    End If
End Sub

If there is other formatting you want done—such as modifying the dimensions of the text box—you can add that information within the macro, as well. Simply add lines that adjust the .Height and .Width properties of the ShapeRange object.

Again, this sort of work with macros can only be done by manually writing or editing them to get exactly what you want—something that cannot be done with the Macro Recorder.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13020) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

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

Technique for Adding a Text Box to an Envelope

Text boxes can be a great design feature to use in laying out a document. You may want to add one to an envelope, ...

Discover More

Cropping Graphics

Excel makes it easy to place a graphic in a worksheet. Once there, you may want to chop off a side (or two) of the ...

Discover More

Creating a Bar Chart for Temperatures

Excel can create a large variety of charts, but sometimes it can take some real creativity to get exactly the chart you ...

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)

Swapping Two Strings

Part of developing macros is learning how to use and manipulate variables. This tip examines a technique you can use to ...

Discover More

Determining the Month of the Year

Your macro code may need to determine the month of the year represented by a particular date. You can find the desired ...

Discover More

Accessing a Footnote Number in VBA

If you are working with a document that includes footnotes, you might use a macro to do some processing of that document. ...

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}] (all 7 characters, in the sequence shown) 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 + 5?

2023-02-04 23:58:12

Tomek

Sorry, missed the first line. Here is the modified macro again:
'------------------
Sub MyBox()
'
' MyBox Macro
'
'
    If Selection.Type = wdSelectionNormal And Selection.Range.ShapeRange.Count = 0 Then
        With Selection
            .CreateTextbox
            .ShapeRange(1).WrapFormat.Type = wdWrapFront
            .ShapeRange(1).Line.Transparency = 1
            .ShapeRange(1).Fill.Transparency = 1
            .Font.ColorIndex = wdBlack
        End With
    Else
        MsgBox "No valid selection!" & vbCr & "or selection includes existing shapes!"
    End If
End Sub


2023-02-04 23:54:56

Tomek

Here is the modified macro:
------------------
       If Selection.Type = wdSelectionNormal And Selection.Range.ShapeRange.Count = 0 Then
                With Selection
                        .CreateTextbox
                        .ShapeRange(1).WrapFormat.Type = wdWrapFront
                        .ShapeRange(1).Line.Transparency = 1
                        .ShapeRange(1).Fill.Transparency = 1
                        .Font.ColorIndex = wdBlack
                End With
        Else
               MsgBox "No valid selection!" & vbCr & "or selection includes existing shapes!"
        End If
End Sub


2023-02-04 23:53:35

Tomek

My suggested conditions will prevent the macro from processing a selection that includes some shapes, or if only shapes were selected.


2023-02-04 23:51:26

Tomek

An even better If statement could be
If Selection.Type = wdSelectionNormal And Selection.Range.ShapeRange.Count = 0 Then


2023-02-04 23:47:17

Tomek

A better If statement could be
If Selection.Type = wdSelectionNormal Then


2023-02-04 23:44:30

Tomek

Somehow my comment of 2023-02-04 15:26:33 was tagged as suspected scam, and after review got scrambled so I will try to re-post it again. However, as my post for some reason was tagged as spam I will try to post it in parts to figure out what causes this tagging..
Allen, please remove my earlier scrambled post.


2023-02-04 15:26:33

Tomek

I would change the condition of the If statement to:        If Selection.Type = wdSelectionNormal Then....This will prevent the macro from trying to modify other shapes, icons, pictures, whether floating or on line. etc., and possibly triggering an error.BTW, if your text selection includes other floating shapes they will be left behind; only text and in-line shapes can be moved inside the text box.You can prevent this by changing the condition to:        If Selection.Type = wdSelectionNormal And Selection.Range.ShapeRange.Count = 0 Then ...The message can the be also modified to:        IMsgBox "No valid selection!" & vbCr & "or selection includes other shapes!"So the modified macro would be like this:'----------------------------Sub MyBox()'' MyBox Macro''        If Selection.Type = wdSelectionNormal And Selection.Range.ShapeRange.Count = 0 Then                With Selection                        .CreateTextbox                        .ShapeRange(1).WrapFormat.Type = wdWrapFront                        .ShapeRange(1).Line.Transparency = 1                       .ShapeRange(1).Fill.Transparency = 1                      .Font.ColorIndex = wdBlack               End With        Else               MsgBox "No valid selection!" & vbCr & "or selection includes other shapes!"        End IfEnd Sub


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.

Videos
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.