In this chapter:
Program Example: Creating Tables
Program Example: Shortcut Key Assignments
Program Example: User Interaction
Elements of Word VBA Programming
Microsoft Word is a word processor of enormous power and flexibility. But despite its powerful feature set, there is a great deal that Word either does not allow you to do or does not allow you to do easily through its user interface. In these cases, we must turn to Word programming.
Let me give you two examples. I can assure you that these are real-life examples, because they happened to me.
In writing this book, I was faced with the problem of turning long columns of words into tables. Word’s built-in ConvertToTable command was not flexible enough for my needs for three reasons.
First, I wanted the items to be placed in the table in column-major order (the first column is filled first) rather than in row-major order (the first row is filled first). Second, I wanted to choose the number of columns based on the number of words (cells), so I needed to know the word count before I made the choice of the number of columns. Finally, I wanted a row at the top of the table for a title.
To illustrate, I wanted to be able to select the following column of words:
Border |
Cell |
Column |
Document |
Font |
Options |
PageSetup |
Paragraph |
ParagraphFormat |
Range |
Row |
Selection |
Style |
Table |
Template |
Window |
and be presented with a dialog box similar to the one shown in Figure 1-1. Then, if I entered 3 for the number of columns and hit the OK button, I wanted Word to automatically construct Table 1-1 for me.
Table 1-1. Table in Column-Major Order
Table | ||
---|---|---|
Border |
PageSetup |
Style |
Cell |
Paragraph |
Table |
Column |
ParagraphFormat |
Template |
Document |
Range |
Window |
Font |
Row | |
Options |
Selection |
I show you how to write the code to perform this little bit of table prestidigitation in Chapter 16, The Table Object. I cannot tell you how many hours of tedious keyboarding this little gem has saved me.
The second example has to do with shortcut keys. I am very fond of shortcut keys, because using the mouse gives me a pain in the shoulder (for which I have spent many hours in physical therapy, by the way). In any case, while experimenting with shortcut keys, I created a shortcut for the FileOpenFile command and assigned it the hotkey combination Alt-O,1, forgetting that Alt-O is used to open the Format menu.
My work on the book (and on the computer in general) was interrupted for several days. When I returned to work, at some point I hit Alt-O, expecting to get the Format menu. But nothing happened. Now I had to figure out why.
The first thing that occurred to me is that I might have reassigned the Alt-O key, but I couldn’t remember how. This left me with three choices. First, I could painstakingly search through each of the hundreds of commands and macros using the Customize Keyboard dialog box, trying to find a keystroke assignment that began with Alt-O. This was my least favorite choice. Second, I could invoke the FindKey method (discussed in Chapter 18, Shortcut Key Bindings) that searches for a command that corresponds to a given hotkey combination. The problem with this choice is that I would need to know the exact keystroke combination. I did try searching for Alt-O, but this returned nothing.
The final (and best) choice was to write a short program that makes a list of all of the current key bindings. This made it easy to spot the offending key assignment. I show you this program in Chapter 18. My advice: Keep this program handy!
Word programming shines when there is a need for user interaction. For instance, in Chapter 20, Custom Dialog Boxes, I construct a simple program that helps create a fax cover sheet by presenting the user with the custom dialog box shown in Figure 1-2. After you fill in this dialog box and click the OK button, the program automatically creates a fax cover sheet with this information. (Incidentally, I made $500 creating this little program for a company because no one at the company knew how to program in Word!)
I hope that these three illustrations have convinced you that Word programming can be very useful. Of course, you can do much more mundane things with Word programs. For instance, you can automate repetitive tasks, such as searching through a document, finding all the headings and placing them in a new document, or printing the statistics on a given document.
In general, the education of a Word programmer breaks down into a few main categories:
- The Visual Basic Editor
First, you need to learn a bit about the environment in which Word programming is done. This is the so-called Visual Basic Editor or Word VBA Integrated Development Environment (IDE for short). We take care of this in Chapters Chapter 3 and Chapter 4.
- The basics of programming in VBA
Next, you need to learn a bit about the basics of the programming language that Word uses. This language is called Visual Basic for Applications or VBA. Actually, VBA is used not only by Microsoft Word, but also by the other major components of the Microsoft Office application suite: Access, Excel, and PowerPoint. It is also used by the standalone programming environment called Visual Basic (or VB). Any application that uses VBA in this way is called a host application for VBA. (There are also a number of non-Microsoft products that use VBA as their underlying programming language. Among the most notable is Visio, a vector-based drawing program.)
We will discuss the basics of the VBA programming language in Chapters Chapter 5–Chapter 8.
- Object models and the Word object model
Each VBA host application (Word, Access, Excel, PowerPoint, and Visual Basic) supplements the basic VBA language by providing an object model to deal with the objects that are particular to that application.
For instance, Word VBA includes the Word object model, which deals with such objects as documents, templates, paragraphs, fonts, headers, tables, and so on. On the other hand, Excel VBA includes the Excel object model, which deals with such objects as workbooks, worksheets, cells, rows, columns, ranges, charts, pivot tables, and so on. Access VBA includes two object models: the Access object model and the DAO object model, which allow the programmer to deal with such objects as database tables, queries, forms, and reports. (To learn more about the Access and DAO object models, see my book Access Database Design and Programming, also published by O’Reilly & Associates.)
Thus, a Word programmer must be familiar with the general notion of an object model and with the Word object model in particular. We discuss object models in general in Chapter 9, Object Models, after which we begin our discussion of the Word object model, which occupies most of the remainder of the book.
Incidentally, the Word object model is quite extensive—the largest among the Office applications, with almost 200 different objects. (The Excel object model is a close second, but 52 of the Excel objects are included only for compatibility with earlier versions of Excel and are on their way out.)
Lest you be too discouraged by the size of the Word object model, I should point out that you need to be familiar with only a handful of objects in order to program meaningfully in Word VBA. Many of the Word objects relate to rather esoteric areas of Word, such as spelling dictionaries, autocorrect entries, captions, endnotes, footnotes, and so on. In fact, as we will see, the vast majority of the “action” is related to just four objects: the Application object, the Document object, the Range object, and the Selection object.
To help you get an overall two-dimensional picture of the Word object model, as well as detailed local views, I have written special object browser software. (The object browser comes with over a dozen other object models as well.)
Whether you are interested in Word programming in order to be more efficient in your own work, or whether you intend to make money writing Word programs for others to use, I think you will enjoy the increased sense of power that you get by knowing how to manipulate Word at the programming level. Indeed, once you have read this book, you will be halfway to being an Excel, Access, and Power-Point programmer as well!
Get Writing Word Macros, Second Edition 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.