Paul McFedries' Web Home


VBA for Office 2000 Unleashed cover VBA for Office 2000 Unleashed

Introduction

Invention is the mother of necessity.
—Thorstein Veblen

With the release of Office 2000, Visual Basic for Applications has now been on the macro programming stage for about 5 years. That's not all that long, even in geek years, so it's more than a little remarkable that VBA has gone from programming upstart to application stalwart in that short time. After all, do you know anyone who still uses the old Excel macro language or WordBasic? Probably not, and for good reason. Trying to pull together even a modest program using those clunker languages was a nasty, brutish, and long process. On the other hand, VBA, with its superb programming environment and easy-to-learn syntax, can handle even the most ambitious application programming chores.

That's not to say, however, that VBA is only good for major projects. As a writer, I use Word constantly, and over the years I've developed dozens of small macros, functions, and forms that streamline or automate repetitive chores. Most of these routines consist of only a few lines of code, and each one saves me only about 30 seconds to a minute, depending on the task. But I use these routines 50 or 100 times a day, so I end up saving myself anywhere from 30 to 90 minutes a day! That's pretty remarkable, but the proof is in the pudding: I can now write far more pages in a day than I used to. (Don't tell my editor!)

Whether your concern is ease-of-use or personal productivity, there's little doubt VBA is the Office programming language of choice. So now all you have to do is learn how to use it, and that's where this book comes in. I take you through all the basic programming constructs, including variables, loops, and the other tools that are part of every coder's arsenal. I also run through every feature found in the VBA Editor and discuss the all-important topic of objects, particularly those objects that form the heart of Word, Excel, PowerPoint, and Access. So even if you've never programmed before, this book will have you slinging code in no time flat. Experienced programmers will appreciate the book's intermediate-to-advanced sections that cover user interfaces, database programming, Internet programming, debugging, and much more.

What's New in VBA 6.0

The version of VBA that ships with Office 2000 is 6.0. This new version isn't radically different from the VBA 5.0 that was featured in Office 97. However, there are hundreds of changes, large and small, so there should be something for everyone. Here's a summary of a few of the major changes that VBA 6.0 brings to the programming table:

Support for new document types: You can use VBA 6.0 to create the new document types supported by Office 2000 (such as the new email message document type used by Word 2000).

New Web Options settings: One of the hallmarks of Office 2000 is the ease with which documents can be moved from the desktop to the Web (or intranet) and back. Certain important aspects of this are controlled by a series of "Web Options," and VBA can control these options programmatically. See Chapter 20, "Internet and Intranet Programming Topics."

Publishing to HTML: Excel 2000 and PowerPoint 2000 enable you to take a subset of a document (such as a range or a series of slides) and publish it in HTML format. You can do this via VBA, as well, and I show you how in Chapter 20.

Access gets the Visual Basic Editor: Access developers will be pleased to hear that the Visual Basic Editor is now fully integrated into the Access interface. See Chapter 9, "VBA and Access 2000."

Outlook supports VBA: Outlook 2000 now offers full VBA support, including the Visual Basic Editor. This means you can now use the program as a base for projects. See Chapter 22, "Email Programming with Outlook."

Script Editor for VBScript: If you want to create VBScript (as subset of VBA) routines for Web pages, Office 2000 now includes a Script Editor application for creating, running, and debugging scripts. See Chapter 21, "HTML Programming: VBScript and the Script Editor."

Digitally sign your projects: To ensure the integrity of the VBA projects you distribute, you can now use a certificate to digitally sign your code. See Chapter 26, "VBA Tips and Techniques."

Support for OLE DB and ActiveX Data Objects (ADO): The future of programmable database access is ADO, and a new object library enables VBA developers to work with ADO. See Chapter 19, "Advanced Database Programming."

New Application object events: Like Excel, the Application objects for Word and PowerPoint now support various events. And, like Excel, you need to use class modules to trap these events. See Chapter 16, "Rolling Your Own Objects with Class Modules."

New menu features: The CommandBar object has been modified to work with the new Office 2000 features, including adaptive menus and the ability to place hyperlinks on menus and toolbars. See Chapter 12, "Creating Custom Menus and Toolbars."

What You Should Know Before Reading This Book

My goal in writing this book was to give you complete coverage of the VBA language, as well as numerous examples for putting the language to good use. Note, however, that this book isn't a programming tutorial per se. So although I cover the entire VBA language, many relatively low-level topics are presented quickly so that I can get to meatier topics. Therefore, although you don't need programming experience to read this book, knowledge of some programming basics would be helpful.

I've tried to keep the chapters focused on the topic at hand and unburdened with long-winded theoretical discussions. For the most part, each chapter gets right down to brass tacks without much fuss and bother. To keep the chapters uncluttered, I've made a few assumptions about what you know and don't know:

  • I assume you have knowledge of rudimentary computer concepts such as files and folders.

  • I assume you're familiar with Windows and that you know how to launch applications and use accessories such as Control Panel.

  • I assume you're comfortable with the basic Windows 95/98/NT 4 interface. This book doesn't tell you how to work with tools such as menus, dialog boxes, and the Help system.

  • I assume you can operate peripherals attached to your computer, such as the keyboard, mouse, printer, and modem.

  • I assume you've installed VBA 6.0 (via Office 2000 or some other VBA-enabled application) and are ready to dive in at a moment's notice.

  • Most of this book's examples involve Office 2000. Therefore, I assume you've used the Office programs for a while and are comfortable working with these programs.

  • I assume you have a brain and are willing to use it.

How This Book Is Organized

To help you find the information you need, this book is divided into eight parts that group related tasks. The next few sections offer a summary of each part.

Part I: Unleashing the VBA Programming Language

The five chapters in Part I provide coverage of the fundamentals of VBA programming. Chapter 1 familiarizes you with the layout of the VBA Editor, and subsequent chapters teach you about variables, operators, expressions, objects, and procedure control.

Part II: Unleashing Microsoft Office Objects

Part II takes an in-depth look at programming all the major Office 97 applications. I'll discuss the objects, properties, methods, and events that are unique to Word, Excel, PowerPoint, and Access.

Part III: Unleashing VBA User Interface Design

The look and feel of your VBA applications is the subject of the three chapters in Part III. I'll begin with some basic methods for interacting with the user, and then I'll show you how to build forms and control them programmatically, how to assign your procedures to menus and toolbars, and how to use VBA to build menus and toolbars via code.

Part IV: Unleashing Application Integration

These days, it's a rare VBA application that operates in splendid isolation. Most applications will require at least a little interaction with software other than the underlying VBA program. Integrating multiple applications is the topic of the four chapters in Part IV. You'll learn how to control other programs directly, how to use Dynamic Data Exchange, how to program OLE and ActiveX objects, how to control programs via Automation, and how to work with class modules.

Part V: Unleashing VBA Database Programming

Working with information stored in databases and tables is a crucial topic in this era of client/server setups and intranet-based organizations. From simple Excel list maintenance chores, I'll progress to programming the powerful Data Access Objects model, working with ODBC, dealing with database security, ActiveX Data Objects, and lots more.

Part VI: Unleashing VBA Internet and Intranet Programming

Part VI shows you how to program VBA's Internet- and intranet-related features. Topics include publishing to HTML, programming hyperlinks, building a custom Web browser, programming Web pages with VBScript, and controlling Outlook's email feature via VBA.

Part VII: Unleashing Advanced VBA Programming

This part of the book presents a hodgepodge of advanced VBA techniques. You'll learn how to trap errors, how to debug your code, how to use the Windows API, how to work with low-level file I/O and compiler directives, read and write Registry data, and digitally sign your VBA projects.

Part VIII: Unleashing VBA Applications

Fine words butter no parsnips, as they say, so I've crammed this book full of useful, real-world examples. Most of these examples highlight a specific feature, so they tend to be short. Here in Part VIII, however, I offer up some longer examples that show you how to put together full-fledged VBA applications.

The Appendixes

I've also tacked on a few extra goodies at the end of this book. The appendixes include a complete listing of VBA's statements and functions (Appendixes A and B), Windows 98's VBScript-driven Windows Scripting Host (Appendix C), the Windows ANSI character set (Appendix D), and an HTML primer (Appendix E).

This Book's Special Features

Visual Basic for Applications for Office 2000 Unleashed is designed to give you the information you need without making you wade through ponderous explanations and interminable technical background. To make your life easier, this book includes various features and conventions that help you get the most out of the book and VBA itself.

Steps: Throughout this book, each VBA task is summarized in step-by-step procedures.

Things you type: Whenever I suggest that you type something, what you type appears in a monospace font.

Commands: I use the following style for application menu commands: File | Open. This means that you pull down the File menu and select the Open command.

Visual Basic keywords: Keywords reserved in the Visual Basic for Applications language appear in monospace type.

Function arguments: Throughout this book, I'll be providing you with the proper syntax for VBA's many functions. These syntax constructions contain arguments that fall into two categories: required (arguments that you must include when calling the function) and optional (arguments that you can skip if you don't need them). To help you differentiate between these types, I'll show required arguments in a bold, italic, monospace font, and I'll show optional arguments in an italic, monospace font.

The code continuation character (): When a line of code is too long to fit on one line of this book, it is broken at a convenient place, and the code continuation character appears at the beginning of the next line.

VBA 6.0 icon The VBA 6.0 icon: This icon highlights features that are new to VBA 6.0.
CD icon The CD icon: This icon tells you that the file being discussed is available on the CD that comes with this book.

This book also uses the following boxes to draw your attention to important (or merely interesting) information.

NOTE
The Note box presents asides that give you more information about the topic under discussion. These tidbits provide extra insights that give you a better understanding of the task at hand. In many cases, they refer you to other sections of the book for more information.

TIP
The Tip box tells you about VBA methods that are easier, faster, or more efficient than the standard methods.

CAUTION
The Caution box tells you about potential accidents waiting to happen. There are always ways to mess things up when you're working with computers. These boxes help you avoid at least some of the pitfalls.

The Acknowledgments Department

Robert Pirsig, in Zen and the Art of Motorcycle Maintenance, wrote that "a person who sees Quality and feels it as he works, is a person who cares." If this book is a quality product (and I immodestly think that it is), it's because the people at Sams editorial cared enough to make it so.

So a round of hearty thanks is in order for all the good people who worked on this project. You'll find them all listed near the front of the book, but I'd like to extend special kudos to the folks I worked with directly: Acquisitions Editor Sharon Cox, Development Editor Tony Amico, Production Editor Dawn Pearson, Copyeditor Linda Morris, Tech Editor Lowell Mauer, and Software Specialist Michael Hunter. I'd also like to thank Bruce Clingaman for his valued input and, last but proverbially not least, all the thousands of readers from the first edition of the book who wrote to me with praise and suggestions.

About the Author

Paul McFedries is a freelance writer with more than 25 years of programming experience. McFedries is the author of more than two dozen computer books that have sold nearly 2 million copies worldwide. His titles include the Sams books Windows 98 Unleashed and Paul McFedries' Windows 98 Unleashed Professional Reference Edition, as well as the Que books The Complete Idiot's Guide to Windows 98 and The Complete Idiot's Guide to Creating an HTML 4 Web Page.


Back to the VBA for Office 2000 Unleashed home page

Copyright © 1995-2008 Paul McFedries and Logophilia Limited