Loading
Word.Tips.Net WordTips (Menu Interface)

Inserting Text with a Macro

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

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.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Ken Endacott    17 Apr 2016, 06:59
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
Mike    17 Apr 2016, 00:37
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
sunwukong    07 Oct 2015, 00:15
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.

Bill    09 Sep 2015, 14:05
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
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.