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
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:
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.
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!
Got a macro that processes or uses styles? You definitely need to know how many styles Word has available in the ...
Discover MoreVariables are used quite frequently in programming, and VBA programming is no exception. Each variable can have a data ...
Discover MoreYour macro may need to determine if the user has overtype mode turned on. You can find out the overtype status easily by ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments