Microsoft Office 97/Visual Basic Programmer's Guide

Introduction


Contents

Welcome to Microsoft® Visual Basic® for Applications version 5.0, the shared development environment that provides you the means to accomplish a wide range of programmatic results — everything from automating individual tasks to creating full­fledged custom applications in Microsoft Office 97. Whatever your programming background — Visual Basic (Standard, Professional, or Enterprise Edition), a previous version of Visual Basic for Applications, WordBasic, Access Basic, XLM, or another programming language — you'll find a number of powerful new capabilities in this version of Visual Basic for Applications, including those described in the following paragraphs.

Programmatic access to the functionality of each Office application   Each Office application exposes its functionality as a set of programmable objects. Using Visual Basic for Applications gives you access to these objects, making it possible for you to do anything in the application programmatically that you can do manually with the user interface.

Consistent syntax across applications   You no longer need to learn a different programming language for each Office application. This makes it much easier for you to apply the skills you acquire while learning to program one application to other Office applications. This uniformity of language also makes it easier to create solutions that involve more than one Office application and to reuse code across applications.

A powerful, full­featured development environment   The integrated development environment of Visual Basic for Applications is available with Microsoft Excel, Microsoft Word, and Microsoft PowerPoint®; it looks exactly the same no matter which of these applications you start it from. This integrated programming environment runs in its own window, and it includes advanced debugging features, property- and code­editing features (including compile­time syntax checking and tools for constructing statements), an enhanced Object Browser, and code organization and tracking features.

Support for ActiveX controls   You now have the ability to add ActiveX controls — prebuilt, reusable software components that have interactive capabilities — to dialog boxes and to embed them in documents.

A new way to create dialog boxes   You can use Microsoft Forms to create custom dialog boxes in any application that supports the integrated development environment.

Support for integration with databases, messaging systems, and the Internet   You have programmatic access to databases (using Data Access Objects, or DAO), to messaging (using the Microsoft Outlook object model), and to the Internet­ready features of the Office applications (using each application's object model).

Top

Getting Started with Visual Basic

Use the following suggestions to get the most from the time you spend learning Visual Basic for Applications (referred to as "Visual Basic" for the remainder of this book).

Learn Microsoft Office first   The more you know about Office, the better prepared you'll be to venture into Visual Basic. Most Visual Basic procedures perform a sequence of actions in Office, and most instructions in a procedure are equivalent to Office commands or actions. Consequently, working with Visual Basic is a little like working with Office without a user interface; instead of choosing commands and selecting options in dialog boxes, you write Visual Basic instructions. The statements and functions you use to write instructions are much easier to understand if you're already familiar with the features they represent in Office.

Also, if you know Office well, you can better answer the question you're most likely to ask yourself when writing a macro: "What's the best way to do this?" People have been known to write long macros for tasks that could have been handled by a single Office command.

Learn what you need, when you need it   Learn what you need for the task at hand. Visual Basic can seem overwhelming at first, particularly if you don't have any experience with programming languages. A great way to begin learning Visual Basic is to investigate how to accomplish a particular task programmatically. As you gain experience writing procedures that automate different types of tasks, you'll cover a lot of ground.

Use the macro recorder   The macro recorder — a feature that's available with Microsoft Excel, Word, and PowerPoint — can record the corresponding Visual Basic instruction for virtually every action you take in Office. You can use the macro recorder to see how actions performed in Office translate into Visual Basic instructions, and vice versa. Also, you'll find that recording part of a macro is often faster and easier than writing out the instructions.

Use Visual Basic Help   Help is a powerful tool for learning Visual Basic. In a Visual Basic module, you can type a keyword and, with the insertion point positioned somewhere in the keyword, press F1 to immediately display the Visual Basic Help topic for that keyword. Most Visual Basic Help topics for keywords include examples you can copy and paste into your macros. For more information, see the following section, "Using Online Help."

Top

Using Online Help

Microsoft Office provides an extensive Help system for the Visual Basic language, the objects that Office supports, and the properties and methods of those objects.

If you clicked Typical when you installed Office, you'll need to run Setup again to install Help for Visual Basic for the applications you want to program in.

You can access Visual Basic Help in any module in the Visual Basic Editor or in a Microsoft Access module in any of the following three ways:

  • Position the insertion point anywhere in an object name, property name, method name, event name, function name, or other keyword you've typed, and then press F1 to get context­sensitive Help for that keyword.

  • Click Microsoft Visual Basic Help (in the Visual Basic Editor) or Microsoft Access Help (in Microsoft Access) on the Help menu. You can then ask the Office Assistant a question, click Search, and click the topic you want to read in the What would you like to do? balloon.

  • Click Object Browser on the View menu, and then either press F1 or click the Help button (the question­mark button above the Members of box) for information about the selected object, method, property, event, or function.

After you've displayed a Help topic, you can click the Help Topics button in the Help window to display the Help Topics dialog box, which contains three tabs: Contents, Index, and Find. You can then either look up a specific topic or Visual Basic term on the Contents or Index tab or perform a full­text search from the Find tab.

Note   In the Visual Basic Editor, clicking Contents and Index on the Help menu displays the contents and index of Help for the Visual Basic Editor itself. From the Contents tab in Visual Basic Editor Help, you can display the contents and index of Visual Basic Help for Microsoft Excel, Word, or PowerPoint by double-clicking the book title that includes the name of the application you're working in (for example, "Microsoft Word Visual Basic Reference"), and then double-clicking the shortcut in that book (for example, "Shortcut to Microsoft Word Visual Basic Reference"). The Help Topics dialog box should reappear, displaying the contents and index for Visual Basic Help for your application.

Top

Other Resources

Following are descriptions of the various resources you can use to get additional information about programming with Visual Basic in Office.

Technical Support Services

Microsoft offers a variety of support options to help you get the most from your Microsoft product. For more information about available support services, see Getting Results with Microsoft Office 97.

For basic technical support outside the United States, contact the Microsoft subsidiary office that serves your area. Microsoft subsidiary offices and the countries they serve are listed in Getting Results with Microsoft Office 97.

Microsoft Office Developer Forum

You can get the latest information about developing custom applications for Office at the Microsoft Office Developer Forum Web site at http://www.microsoft.com/officedev/

Microsoft Press Books

In addition to the Microsoft Office 97/Visual Basic Programmer's Guide, Microsoft Press® offers a number of books to help you get started programming in Visual Basic. These books help you learn how to automate Office tasks and create custom applications as easily and as quickly as possible. The easy­to­follow lessons include clear objectives and real­world business examples so that you can learn exactly what you need to know, at your own speed.

  • Microsoft Word 97/Visual Basic Step by Step, ISBN 1­57231­388­9, by Michael Halvorson and Chris Kinata

  • Microsoft Excel 97/Visual Basic Step by Step, ISBN 1­57231­318­8, by Reed Jacobson

  • Microsoft Access 97/Visual Basic Step by Step, ISBN 1­57231­319­6, by Evan Callahan

  • Microsoft Office 97/Visual Basic Step by Step, ISBN: 1­57231­389­7, by David Boctor

For a technical exploration of the wide range of line-of-business development opportunities available to Office 97 developers, see the Microsoft Office 97 Developer's Handbook, ISBN 1-57231-440-0, by Christine Solomon.

Building Microsoft Outlook 97 Applications, ISBN 1-57231-5736-9, by Peter Krebs is a results-oriented book that offers both the nonprogrammer and the experienced IS professional the information, strategies, and sample applications they need to get started building useful groupware and mail-enabled applications.

For information about other Microsoft Press titles, see the Microsoft Press Web site at http://www.microsoft.com/mspress/

Mastering Office 97 Development

Mastering Office 97 Development is a CD-ROM product available from Microsoft. Use this self-paced training tool to develop real-world skills that you can put to work right away. Become proficient with Visual Basic for Applications, Office 97 object modules, and more. More than 40 hours of labs, demos, sample code, and articles — plus valuable tips and techniques — get you up to speed fast. Use the powerful Boolean search engine and comprehensive index to find just information you need when you need it. Narrated demonstrations and interactive lab exercises walk you through complex concepts and help you design your own Office 97-based applications.

Top

How This Book Is Organized

The chapters in this book cover basic concepts pertaining to object models and Visual Basic programming, the object models for the Office applications, and major feature areas of Visual Basic.

Chapter 1, "Programming Basics," provides a brief overview of the mechanics of writing Visual Basic code.

Chapter 2, "Understanding Object Models," is an introduction to the concept of programmable object models.

Chapter 3, "Microsoft Access Objects," discusses the Microsoft Access object model in detail.

Chapter 4, "Microsoft Excel Objects," discusses the Microsoft Excel object model in detail.

Chapter 5, "Microsoft Outlook Objects," discusses the Microsoft Outlook object model in detail.

Chapter 6, "Microsoft PowerPoint Objects," discusses the Microsoft PowerPoint object model in detail.

Chapter 7, "Microsoft Word Objects," discusses the Microsoft Word object model in detail.

Chapter 8, "Menus and Toolbars," and Chapter 12, "ActiveX Controls and Dialog Boxes," show you how to add interactive, custom user­interface elements to your Visual Basic applications.

Chapter 9, "Microsoft Office Assistant" discusses the Office Assistant object model in detail.

Chapter 10, "Shapes and the Drawing Layer," discusses the Office Art object model in detail.

Chapter 11, "Data Access Objects," explains how to use Data Access Objects (DAO) to import and export information stored in a database.

Chapter 13, "Optimizing for Size and Speed," provides several easy techniques that can make your Visual Basic code faster and more concise.

Chapter 14, "Debugging and Error Handling," shows you how to find and eliminate bugs in your code before you run it and how to handle errors that occur while your code is running.

Chapter 15, "Developing Applications for the Internet and World Wide Web," provides information about how to control the new Internet­ready features in Office applications programmatically.

The appendixes provide helpful information for experienced users of XLM or WordBasic who are switching to Visual Basic.

Top

Document Conventions

This book uses the typographic conventions listed in the following table. You might not recognize all the terms or Visual Basic keywords yet, but you'll learn more about them later.

Example of convention Description
setup Words or characters you're instructed to type are formatted as bold.
Sub, If, ChDir, MsgBox, True, Add, Height, Application, Range, Row Bold words with the initial-letter capitalization indicate either a language-specific term (a property, method, event, or object name), another Visual Basic keyword, or an interface element (such as a menu command or a toolbar button).
objectIn text, italic type indicates important new terms, usually the first time they occur in the book.
PropertyNameIn code syntax, italic type indicates placeholders for information you're to supply.
ENTERSmall capital letters are used for the names of keys and key combinations, such as ENTER and CTRL+R.
CTRL+VA plus sign (+) between key names indicates a key combination, or shortcut keys. For example, CTRL+V means to hold down the CTRL key while pressing the V key.
DOWN ARROWIndividual arrow keys are referred to by the direction of the arrow on the key (LEFT, RIGHT, UP, or DOWN). The phrase "arrow keys" is used to describe these keys collectively.
BACKSPACE, HOMEOther navigational keys are referred to by their specific names.
myVar This font is used for example code.
Sub StockSale ( )
   .
   .
   .
End Sub
A column of three periods indicates that part of an example has been intentionally omitted.



Top