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

Engineering Calculations

Need to normalize your data in some way so that all your values are in a given format? This tip presents a number of ...

Discover More

Extra Document File Being Saved

You may be surprised sometime to save a document and find out that Word actually saves what appear to be two copies of ...

Discover More

Understanding Auditing

Excel provides some great tools that can help you see the relationships between the formulas in your worksheets. These ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Converting to Lowercase and Small Caps

Word has a powerful Find and Replace capability. If you want to change the case of what is found, however, then Find and ...

Discover More

Deleting a Macro

Macros are often created to accomplish a specific task, after which they are no longer needed. If you need to delete a ...

Discover More

Running Macros Based on Keywords

Wouldn't it be great if Word could execute a macro every time someone typed in a particular keyword or phrase? Word may ...

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 two more than 7?

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.