Writing Word Macros

ISBN: 1-56592-725-7, 410 pp., $29.95

Return to Roman Press Home Page
Return to computer books' main page

To order this book through Amazon.com, please click here


A Title Change and Word 2000 Compliance

After careful consideration, the folks at O'Reilly and I have decided that the original title of this book: Learning Word Programming was probably not the best choice of title. I am convinced that many Word users can benefit from a knowledge of how to create macros using the Visual Basic editor. However, the word "Programming" in the previous title may have been a bit intimidating to some Word users.

Writing Word macros is probably not for the casual Word user, but if you are a semi-serious to serious user of Word, then this book can help you get considerably more power out of this application.

Accordingly, we have changed the title of the book to the more user-friendly Writing Word Macros. This also brings the title in line with my book Writing Excel Macros (also published by O'Reilly).

I have also taken this opportunity to make sure that the book is up to date with respect to the release of Word 2000. The Word 2000 object model differs little from its predecessor. There are only a handful of new objects, mostly related to the Internet. The main change for us is the treatment of the Documents collection. This change is due to the fact that Word 2000 is now a more-or-less single document interface application, that is, each instance of Word 2000 can have only one document open at a time. (Actually, the situation is somewhat more involved and is discussed in detail in Chapter 11.)


About the Book

In the old days, when you wanted to perform a repetitve task with a word processing application such as Microsoft Word, you would record a macro, that is, a series of keystrokes that could be played back using a special hotkey. These days, Microsoft Word (as well as all of the major Office applications, including Excel, Access and PowerPoint) use the Visual Basic for Applications (VBA) programming language as their macro language. Using VBA, you can write simple or sophisticated programs (macros) to control Microsoft Word. This control goes beyond creating the simple old-style keystroke recordings. In fact, nearly every aspect of Word can be controlled through the VBA programming language. Moreover, the VBA programming language allows you to endow your macros with decision making capabilities! This gives far more powerful control over Word than simple keystroke recordings.

By writing a Word VBA program, you can tap into the powerful Word object model in order to control every aspect of Word. Examples of objects in the Word object model are: documents, templates, styles, paragraphs, fonts, tables and lists.

The purpose of Writing Word Macros is to provide you with the necessary skills to write Word VBA programs. No previous knowledge of VBA programming is necessary to read this book. On the other hand, the book is sufficiently detailed to give you a solid and thorough understanding of both the VBA language and the Word object model.

Writing Word Macros focuses on:

Personally, I hate long, wordy, overblown 1000+ page books half of which seem to be devoted to the author's "humorous" anecdotes, so I wrote Writing Word Macros in a terse, no-nonsense manner that is characteristic of all my books. Instead of a slow-paced tutorial with a lot of hand-holding, I tried to give you the insight you need to program effectively. The book includes several useful examples that solve practical programming problems, like generating tables of a particular format, managing shortcut keys, creating fax cover sheets, and reformatting documents.


Preface From Writing Word Macros

As the title suggests, this book is intended for those who want to learn how to program Microsoft Word 97 or later.

I guess that I cannot avoid dealing at the outset with the question, "Why would anyone want to program Microsoft Word?" The answer is simple: to get more power out of this formidable application. As you will see, there are many things that you can do at the programming level that you cannot do at the user-interface level, that is, with the menus and dialog boxes of Word. Chapter 1 provides some concrete examples of this.

This book provides an introduction to programming the Word object model using Visual Basic for Applications (VBA). However, it is not intended to be an encyclopedia of Word programming. The goal here is to acquaint you with the main points of Word programming-enough so that you can continue your education (as we all do) on your own. The point is that, after reading this book, you should not need to rely on any other source except the Word VBA help file or a good Word VBA reference book (such as O'Reilly's forthcoming Programming the Word Object Model, by Julianne Sharer and Arthur Einhorn) and a nice object browser (such as my object browser, a coupon for which is included in the back of this book).

It has been my experience that introductory programming books (and, sadly, most trade computer books) tend to do a great deal of handholding (to put the matter euphemistically). They cover concepts at a very slow pace primarily by padding them heavily with overblown examples and irrelevant anecdotes that only the author could conceivably find amusing, especially the second or third time that we are forced to read them while looking for a few facts. Frankly, I find such unprofessionalism incredibly infuriating. In my opinion, it does the reader a great disservice to take perhaps 400 pages of information and pad it with another 600 pages of irrelevant junk.

There is no doubt in my mind that we need much more professionalism from our authors, but it is not easy to find writers who have both the knowledge to write about a subject and the training (or talent) to do so in a pedagogical manner. (I should hasten to add that there are a number of excellent authors in this area-it's just that there are not nearly enough of them.) Moreover, publishers tend to encourage the creation of 1000 page plus tombs because of the general feeling among the publishers that a book must be physically wide enough to stand out on the bookshelf! I shudder to think that this might, in fact, be true. (I am happy to say that O'Reilly does not seem to have succumbed to this opinion.)

On the other hand, Writing Word Macros is not a book in which you will find much handholding. (Nor will you find much handholding in any of my books.) The book proceeds at a relatively rapid pace from a general introduction to programming, through an examination of the Visual Basic for Applications programming language to an overview of the Word object model. Given the enormity of the subject, not everything is covered, nor should it be. Nevertheless, the essentials of both the VBA language and the Word object model are covered so that, when you have finished the book, you will know enough about Word VBA to begin creating effective working programs.

I have tried to put my experience as a professor (about 20 years) and my experience writing books (about 30 of them) to work here to create a true learning tool for my readers. Hopefully, this is a book that can be read (perhaps more than once) and also serve as a useful reference.


Preface

1. Introduction

2. Preliminaries
     What Is a Programming Language?
     Programming Style

I. The VBA Environment 15

3. The Visual Basic Editor, Part I
     The Project Window
     The Properties Window
     The Code Window
     The Immediate Window
     Arranging Windows
     Document Events

4. The Visual Basic Editor, Part II
     Navigating the IDE
     Getting Help
     Creating a Procedure
     Run Mode, Design Mode, and Break Mode
     Errors
     Debugging
     Macros

II. The VBA Programming Language 49

5. Variables, Data Types, and Constants
     Comments
     Line Continuation
     Constants
     Variables and Data Types
     VBA Operators

6. Functions and Subroutines
     Calling Functions
     Calling Subroutines
     Parameters and Arguments
     Exiting a Procedure
     Public and Private Procedures
     Referencing a Project

7. Built-in Functions and Statements
     The MsgBox Function
     The InputBox Function
     VBA String Functions
     Miscellaneous Functions and Statements

8. Control Statements
     The If - Then Statement
     The For Loop
     The For Each Loop
     The Do Loop
     The Select Case Statement
     A Final Note on VBA

III. Objects and Object Models 107

9. Object Models
     Objects, Properties, and Methods
     Collection Objects
     Object Model Hierarchies
     Object Model Syntax
     Object Variables

10. The Word Object Model
     The Word Object Model: A Perspective
     Word Enums
     The VBA Object Browser

11. The Application Object
     Properties and Methods
     The Options Object
     The Task Object
     The Template Object
     The Window Object
     The Pane Object
     The View Object
     The Zoom Object

12. The Document Object
     Properties That Return Collections
     Spelling-Related Properties and Methods
     The Documents Collection
     Adding, Opening, and Saving Documents
     Password-Related Properties
     Protection-Related Properties and Methods
     The Name Properties
     Printing-Related Methods
     Additional Members of the Document Object
     Children of the Document Object
     Example: Printing Document Headings
     Example: Finding Used Styles

13. The Section and HeaderFooter Objects
     Adding a New Section
     The PageSetup Object
     Properties of the Section Object
     The HeaderFooter Object

14. The Range and Selection Objects
     Comparing the Range and Selection Objects
     Range and Selection Variables
     Creating a Range or Selection Object
     Changing a Range Object
     Range and Selection Object Properties and Methods

15. The Find and Replace Objects
     Searching for Text
     Searching for Formatting
     The Replace Operation
     The Execute Method
     Example: Repeated Searching

16. The Table Object
     Formatting-Related Properties and Methods
     The Cell Method
     The Columns and Rows Properties
     The ConvertToText Method
     Sorting Methods
     The Split Method
     Example: Creating Tables from Word Lists
     Example: Closing Up a Table

17. The List Object
     List Types
     The List Object
     The ListTemplate Object and ListGalleries
     Example: Looking at Lists

18. Shortcut Key Bindings
     Finding a Key Binding
     Creating a Key Binding
     The KeyBinding Object

19. Built-in Dialog Objects
     The Show Method
     The Display and Execute Methods
     The DefaultTab Property
     The Type Property
     The Update Method
     Example: Printing Document Statistics

20. Custom Dialog Boxes
     What Is a UserForm Object?
     Example: Adding a Closing to a Letter
     Example: A Fax Cover Sheet
     Example: Quick Selection

21. Menus and Toolbars
     An Overview
     CustomizationContext
     The CommandBars Collection
     The CommandBar Object
     Command Bar Controls

IV. Appendixes 341

A. Programming Word from Another Application

B. The Shape Object

C. Getting the Installed Printers

D. High-Level and Low-Level Languages


To order this book through Amazon.com, please click here
Return to Roman Press Home Page
Return to computer books' main page