O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

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

The Book’s Sample Code

When reading this book, you will encounter many small programming examples to illustrate the concepts. I use small coding examples, if possible, to illustrate a point.

Personally, I seem to learn much more quickly and easily by tinkering with and tracing through short program segments than by studying a long, detailed example that has a lot of irrelevant stuff in it, as many do. Also, the difficulty in tinkering with a long program is that changing a few lines can affect other portions of the code to the point where the program will no longer run. Then you have to waste time trying to figure out why it won’t run.

On the other hand, if, for example, I want to figure out exactly what Word VBA thinks is a word (you might be surprised), I write a short program of perhaps a half-dozen lines with which to experiment. This is a great way to learn, is often much quicker than hunting through online documentation (which may very well not have the answer anyway), and also shows off the advantages of an interpreted language (a language that is easily traced one line at a time). [1]

I encourage you to follow along with the code examples by typing them yourself. (Nevertheless, if you’d rather save yourself the typing, sample programs are available online; see “Obtaining the Sample Programs” later in this preface.) Also, I encourage you to experiment; it is definitely the best way to learn.

However, I must add a small caveat. Word programming is very powerful. To protect yourself, I strongly suggest that you do two things:

  • Make a new Word template and store all sample programs in this template, or in documents based on this template. (I discuss how to store programs in templates or documents in Chapters Chapter 3 and Chapter 4, which cover the Visual Basic Editor.) The point is that you do not want to use your Normal template or any other templates that you cannot afford to delete if necessary. (Once you are happy with a program and think you might want to use it for real work, you can copy it to your Normal template.)

  • Back up all of your templates, especially your Normal template (normal.dot), before beginning your Word programming career.

Incidentally, you do not need to worry about doing any damage to your computer. The only damage that you can do in experimenting with Word programming (under normal circumstances) is to a Word template or document. If you have backed up your templates, you should be safe.

One final comment about the sample code is worth making, particularly because this book and its coding examples are intended to teach you how to write VBA programs for Microsoft Word. Generally speaking, there is somewhat of a horse-before-the-cart problem in trying to write about a complicated object model, because it is almost impossible to give examples of one object and its properties and methods without referring to other objects that may not yet have been discussed.

As an example, when I discuss the Bookmark object in Chapter 11, I also must discuss the Add method, used to create new bookmarks. This method has the following syntax:

BookmarksObject.Add(Name, Range)

where Range represents a Range object, which we will have not yet discussed officially.

Frankly, I don’t see any way to avoid this problem completely, so rather than try to rearrange the material in an unnatural way, it seems better to simply proceed in an orderly fashion. Occasionally, we will need to refer to objects that we have not yet discussed, but this should not cause any serious problems, because most of these forward references are fairly obvious.

Obtaining the Sample Programs

The sample programs presented in the book are available online and can be freely downloaded from our web site at http://www.oreilly.com/catalog/writewordmacro/.

[1] Incidentally, in case you are curious: for Word VBA, a word includes any trailing spaces (even more than one space), if present. Also, a punctuation mark is considered a word.

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