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

Get Special Edition Using Microsoft® Office XP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.