O'Reilly logo

Special Edition Using Microsoft® Office XP by Woody Leonhard, Ed Bott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using VBA to Add Text

You would think that something as simple as adding text to a document, spreadsheet cell, slide body, or an e-mail message should be fairly uniform, and that all the Office applications would behave in more or less the same way.

Not a chance.

Adding Text to a Word Document with VBA

In VBA/Word, the simplest way to add text to a document is with the TypeText method. For example, the small program in Listing 40.1 creates a new document, adds a return address and date, and then exits, allowing the typist to continue where the macro left off.

Listing 40.1. TypeReturnAddress
 Public Sub TypeReturnAddress() Documents.Add Selection.TypeText "Ed & Woody's Widgets" Selection.TypeParagraph Selection.TypeText "123 Anyplace" Selection.TypeParagraph ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required