Chapter 1. Preface
Describing Microsoft Office Word as just a word processor understates the capabilities of this versatile application, which is used by nearly a billion people worldwide. The designers anticipated a broad range of possible uses and planned wide latitude for customization. Word is now part word processor, spreadsheet, database, and Web editor. It's also a programming platform.
After the introduction of the Visual Basic editor in Word 97, Word gained popularity for its ability to automate the most mundane and repetitious tasks. Complex calculations can be performed in automatically updating containers (called fields ), and strings of tasks can be chained together into a single operation (called a macro ).
This book introduces two forms of Word automation:
- User interface: This form of automation uses items that can be selected from the user ribbon or placed in the document. Examples include user forms, fields, Quick Parts (new in Office 2007), and user controls such as text input areas, and checkboxes.
- Macros: The other form of automation, macros, is perhaps less familiar. Macros let you record and recall tasks so you don't have to repeat them step-by-step. Your use of macros can include: running ones others provide to you, recording your own, modifying recorded macros, and programming macros.
Each chapter focuses on one aspect of automation, providing examples, techniques, and where applicable, macros.
1.1. Who Should Read This Book
This book is for Word users who want more control and insight into Word's automation capabilities. Automation was never intended to only be for programmers. Instead, automation is equally useful for writers. To be clear, this book is not about programming or making you a programmer. I understand not everyone is interested in becoming a programmer just to accomplish everyday tasks. Instead, I intend to jump start the automation process and enable you to write automation solutions yourself.
I assume you have some experience with Word. Although many of the examples include detailed step-by-step procedures, this is not a Word tutorial. Automation is partly about stringing Word functions together, but knowing what those functions are their options, the right combination, and the right sequence is up to you. Seasoned Word users, or power users, can benefit from this book, but so can less experienced users.
1.1.1. Word Versions
Most of the examples and topics are applicable to all versions of Word, including Word 97, Word 2000, Word XP, Word 2003, Word 2007, and Word 2010. Where this is not the case, I will state which versions apply. For example, Building Blocks and Quick Parts were introduced in Word 2007 and will not be applicable to earlier versions.
1.2. How to Use This Book
This book takes a pragmatic and realistic approach to learning automation. It is part cookbook and part tutorial. I encourage you to work through the examples line by line. For code examples, you can type the code into the integrated development environment (IDE) or copy and paste from the electronic samples provided on this book's website. Feel free to experiment and try variations.
The coding style and examples are different throughout the book, allowing you to see differences and make your own choices. For example, unless otherwise noted, samples use For...Next
loops and For...Each
loops interchangeably. Since the automation challenges differ for each writer, no book of examples can address them all. Instead, study several examples and use whichever examples and code work best to construct the solution you need.
1.3. Book Structure
This book uses a tutorial and cookbook approach to introduce Word's automation concepts. Each chapter introduces a new aspect of automation. I first explore basic procedures, followed by automation techniques, and finally VBA (Visual Basic for Applications) with code examples. VBA can almost always be used to supplement automation techniques available through the user interface. That means you can choose not to use macros at all, too.
Although I assume you have some experience with Word and the user interface, I do not assume any programming experience. If you want to get the most from using VBA, you should read and understand the first two chapters before reading other chapters. They explain basic automation principles and creating and modifying macros. The remaining chapters may be read in any order.
- Chapter 2: Introduces the basics of automation when to use it and when not to and discusses automation project management.
- Chapter 3: Covers recording and modifying macros from the Word code generator. Word can record and playback a series of user actions in procedures called macros. Recorded macros can be modified, or new macros can be created by hand. The chapter discusses each of these techniques, which will be used throughout the rest of the book.
- Chapter 4: Covers Find and Replace. This is a versatile utility that goes well beyond simply finding and replacing text. You can set up powerful replacements that can convert the messiest of Word HTML into clean XHTML code. It also provides a powerful search language that uses wildcards, to find patterns of text, such as telephone numbers or part numbers.
- Chapter 5: Covers fields and form fields. Fields are automatically updating containers of text. Most readers know that page numbers, like Page 1 of 10 , are fields, but you may not know that you can craft your own fields to perform a wide range of calculations. Word tables can be used as small Excel-like spreadsheets, incorporating many of the same functions. Combined with other features, including bookmarks and macros, an impressive amount of information can be created, collected, or shared with minimal effort.
- Chapter 6: Covers the automatic placement or insertion of information. These features include AutoCorrect, which automatically fixes spelling errors for example, switching teh for the and an expanded feature in Office 2007, Building Blocks, which allows you to insert any amount of material from a client's name to an entire document template complete with formatting and styling into a Word document.
- Chapter 7: Covers Smart Tags, a capability that automatically highlights predefined terms or patterns in the text, such as names, acronyms, or addresses, with a purple underline like the underline used to flag misspelled words. Clicking on a highlighted term presents additional actions for that term, such as a link to a company website or a search page. Smart Tags terms and actions can also be linked with macros to help in collecting or identifying those terms. The chapter covers using default Smart Tags, creating your own, and creating and maintaining the terms list.
- Chapter 8: Covers the different ways that information can be imported to, exported from, or changed by Word. Word includes many features that go beyond common copy and paste. Dynamic links can be established to other documents; Word can query and collect data from MySQL, Oracle, and Microsoft Access databases; and Word can poll spreadsheets and documents, including plain text files.