|
Visual Basic for Applications Unleashed
Introduction
Invention is the mother of necessity. When Excel 5 was released in 1994, developers and power users were thrilled that it included the first incarnation of Microsoft's long-awaited new macro language: Visual Basic for Applications (VBA). Many people discarded the creaky old Excel 4 macro language without a second thought and waited eagerly for Microsoft to fulfill its promise to make VBA the common macro language for all its applications. Other Office products were slowly brought into the VBA fold, but it's only with the release of Office 97 that Microsoft has finally achieved its goal. Now all of the Big FourWord, Excel, Access, and PowerPointhave a VBA development environment at their core. Not only that, but just about everything in the Office 97 package is programmable: Outlook, Office Binder, even the Office Assistant. But unprecedented programmatic control over Office objects is only the beginning. Microsoft is now licensing VBA as a separate productcalled VBA 5.0that other companies can incorporate into their own applications. A number of developers have leaped willingly onto the VBA 5.0 bandwagon, including such heavyweights as Adobe, Autodesk, Micrografx, and Visio. And as if that weren't enough, Microsoft has also created a version of VBAcalled VBScriptthat programmers can use as a scripting tool for Web pages. There is little doubt, then, that VBA plays a huge role in Microsoft's future plans. Anyone interested in truly unleashing the power of the Office applications, other programs, and Web pages will need to learn the VBA language. The good news is that VBA combines both power and ease of use. So even if you've never programmed before, you won't find it hard to create useful procedures that let your applications perform as they never have before.
VBA 5.0: Something New for EveryoneVBA 5.0 is no mere incremental upgrade. Since the previous version was released with Office 95, Microsoft has spent its time revamping the interface and cramming the VBA tool chest with countless new programming gadgets and gewgaws. (In case you're wondering, the "5.0" designation doesn't mean all that much. It just synchronizes the version numbers of VBA and the latest incarnation of Visual Basic.) I'll be showing you how to take advantage of these new features throughout this book, but let's begin with a sneak preview so you'll know what to expect:
What You Should Know Before Reading This BookMy 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 we 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:
How This Book Is OrganizedTo 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: Introducing VBAThe five chapters in Part I provide intermediate-to-advanced coverage of the fundamentals of VBA programming. Chapter 1 familiarizes you with the layout of the new VBA Editor, and subsequent chapters teach you about variables, operators, expressions, objects, and procedure control.
Part II: Unleashing Microsoft Office ObjectsPart 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 DesignThe look and feel of your VBA applications is the subject of the three chapters in Part III. We'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 IntegrationThese 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 OLE Automation, and how to work with class modules.
Part V: VBA Database ProgrammingWorking 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, we'll progress to programming the powerful Data Access Objects model, working with ODBC, dealing with database security, and lots more.
Part VI: Unleashing VBA Internet and Intranet ProgrammingPart VI shows you how to program VBA's Internet- and intranet-related features. Topics include programming hyperlinks, building a custom Web browser, taking advantage of Outlook's built-in groupware features, building Web pages with the ActiveX Control Pad, and programming Web pages with VBScript.
Part VII: Unleashing Advanced VBA ProgrammingThis 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, and how to work with low-level file I/O and compiler directives.
Part VIII: Unleashing VBA ApplicationsFine 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 AppendixesI'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), the Windows ANSI character set (Appendix C), an HTML primer (Appendix D), and some material related to the CD that comes with this book (Appendix E).
About the CDThis book comes with a CD-ROM that contains the following:
This Book's Special FeaturesVisual Basic for Applications 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.
The Acknowledgments DepartmentRobert 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.I would therefore like to thank all of the people who helped make this book a reality. I'd like to begin by thanking Acquisitions Editor Dean Miller for, well, acquiring me and for pulling the project together. Many thanks go to Development Editor Brian-Kent Proffitt for doing a great job and for showing compassion to a sore-wristed author. As always, I reserve some special thank-yous for Production Editor Gayle Johnson who, more than anyone I know in publishing, sees Quality and feels it as she works. I'd also like to thank Copy Editor Bart Reed for his uncanny ability to find my dumb mistakes, and Technical Editor John Charlesworth for making sure my instructions and code cut the technical mustard. Finally, a big thank-you goes out to Software Development Specialist Patty Brooks for putting together the excellent CD that appears in the back of the book.
Online Resources for This BookIn this age of global communications and speed-of-light conversations, the notion that the relationship between a writer and his readers will go no further than the book you now hold is rather quaint, if not downright antiquated. This is particularly true when the book's subject matter is programming, because learning how to control these unruly electronic beasts through code is a lifelong process. To that end, I've assembled several online resources that you can use to further your VBA education and commune with like-minded souls.For starters, if you have any comments about this book, or if you want to register a complaint or a compliment (I prefer the latter), please don't hesitate to send an e-mail missive to the following address: Better yet, feel free to drop by my Web site, have a look around, and sign the Guest Book: Note that I have a home page for Visual Basic for Applications Unleashed at this site. Here you'll find book excerpts and info, code samples, links to VBA information, programs and utilities written by other readers, and lots more. To go straight there, dial the following address into your Web browser: http://www.mcfedries.com/books/VBAUnleashed/
About the AuthorPaul McFedries is a computer consultant, programmer, and freelance writer. He has worked with computers in one form or another since 1975, he has a degree in mathematics, and he can swap out a hard drive in seconds flatyet still, inexplicably, he has a life. McFedries is the author or coauthor of more than two dozen computer books that have sold nearly 1 million copies worldwide. His titles include Paul McFedries' Windows 95 Unleashed, Microsoft Office 97 Unleashed, and Navigating the Internet, all from Sams Publishing.Other hats worn by McFedries on occasion include video editor, animator, bread maker, Webmaster, brewmaster, cruciverbalist, and neologist. He has no cats, and his favorite hobbies are shooting pool, taking naps, riding his motorcycle, and talking about himself in the third person. Back to the VBA Unleashed home page
Copyright © 1995-2012 Paul McFedries and Logophilia Limited |