Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Inserting Text with a Macro.

Inserting Text with a Macro

Written by Allen Wyatt (last updated June 10, 2019)
This tip applies to Word 97, 2000, 2002, and 2003


6

Inserting text is a common occurrence in macros. You may need the macro to insert numbers, do some standardization on wording, or some other text insertion. The following VBA code lines each insert a different type of text:

Selection.TypeText Text:="this text"
Selection.TypeText Text:=MyText
Selection.InsertParagraph

Each of the three lines inserts something at the current insertion point. The first line inserts everything between the quote marks, the second inserts the value of the MyText variable, and the third inserts a paragraph mark (an empty line).

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 (1406) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Inserting Text with a Macro.

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

Using the SUBTOTAL Function

Need to sum up different ranges of cells? One of the tools you can use is the handy SUBTOTAL function, described in this tip.

Discover More

How Many Rows and Columns Have I Selected?

Want a quick way to tell how may rows and columns you've selected? Here's what I do when I need to know that information.

Discover More

Using VLOOKUP to Access Information to the Left

One of the most useful function in Excel is VLOOKUP. One thing it won't do, however, is allow you to lookup information ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (menu)

Finding and Changing Word's Internal Commands

If you know how to create macros, you can easily create entire replacements for Word's internal commands. Here's all you ...

Discover More

Setting the Right Indent of a Paragraph in a Macro

Need to format your document using a macro? You can easily set the right margin for an individual paragraph by using the ...

Discover More

Calculating a Future Date

Need to figure out a date a certain number of days, weeks, months, or years in the future? It's easy to do using the ...

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-10-03 15:44:28

Thomas Bottoms

I am trying to format cells and insert text (a checkbox) in each SELECTED cell in a table. This is for specific place keeping in my plant emergency procedures. The cells format properly, but the text will only insert in the FIRST selected cell. I do not know how to create a loop to go to each selected cell. I can't just tell it a specific column or row, because the areas where checkboxes are needed can be scattered all over the place due to the nature of the procedures. I need to be able to manually select multiple cells.

Example VBA:

Sub CkBox2()
'
' CkBox2 Macro
' Inserts check box in each SELECTED cell.
'
Selection.Style = ActiveDocument.Styles("Normal")
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.ParagraphFormat.SpaceBefore = 6
Selection.ParagraphFormat.SpaceAfter = 6

Selection.Font.Size = 14
Selection.TypeText Text:=""
Selection.InsertSymbol Font:="Wingdings", CharacterNumber:=168, Unicode:=True

End Sub

Using this, I end up with: (see Figure 1 below)

Figure 1. Result of my macro


2021-04-20 05:22:10

Peter

I can't solve the following problem: I need to find and then move a piece of text using VBA. The text is formatted (fragments are marked with italic).

How do I do it:

footText = Selection.Text
...
[another] Selection.Text = footText

Of course, only text is transferred. Is it possible to transfer the formatting in the found text as well?


2016-04-17 06:59:36

Ken Endacott

Mike,
This should do what you want.

Sub LeftAndRight()
Dim leftString As String
Dim rightString As String
Dim printWidth As Single
With ActiveDocument.PageSetup
printWidth = .PageWidth - .LeftMargin - .RightMargin - .Gutter
End With

loopback:
leftString = InputBox("Text to the left")
If leftString = "" Then Exit Sub
rightString = InputBox("Text to the right")
If rightString = "" Then Exit Sub
Selection.TypeText leftString & Chr(9) & rightString
With Selection.Paragraphs(1).TabStops
.ClearAll
.Add Position:=printWidth, _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
End With
Selection.TypeText vbCrLf
GoTo loopback
End Sub


2016-04-17 00:37:56

Mike

I would like to type text left aligned, then type different text right aligned, on the same line, from VBA. Do you know how to do that?

Thanks,
Mike


2015-10-07 00:15:49

sunwukong

I have text like this aaa bbb ccc U+21d73

I would like to be able to search thru the text and copy the 21d73 part and a trailing space just after the U+21d73 part and then insert the 21d73 part and then highlight only that part and once selected reveal / convert it to a Chinese Character (CTRL+X). Then loop back to find the next U+ character code.


2015-09-09 14:05:00

Bill

Well, this used to work great for word 2010 to insert standard comments into a work comment field. Now with 2013, this is, of course, broken. "the typetext method or property is not avaialble because the current selection is editing a non editable portion of the reviewing pane" which is a complete piece of bunk; it *is* editing the editable portion of the reviewing pane, as evidenced by my typing into it. More de-contented SW from the people who thoguht nausea-inducing cursors were more useful than macros. Arrgh


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.