Reading a Visual Basic Program Statement

As you learned in the last chapter, a line of code in a Visual Basic program is called a program statement. A program statement is any combination of Visual Basic keywords, objects, properties, methods, functions, operators, and symbols that collectively create a valid instruction recognized by the Office macro interpreter. A complete program statement can be a simple keyword such as

Beep

which sounds a note from your computer's speaker, or it can be a combination of elements, such as the Microsoft Word program statement shown in Figure 38-1, which uses the TypeText method of the Selection object to insert the text Outdoor Clothing Supply into the current document.

click to view at full size.

Figure 38-1. A typical Visual Basic program statement.

The rules of construction that you must use when you build a program statement are called statement syntax. Office Visual Basic (also called Visual Basic for Applications) shares many of its syntax rules with earlier versions of the Basic programming language and with other language compilers. The trick to writing good program statements is learning the syntax of the most useful language elements and then using those elements correctly, in conjunction with programming constructs called objects, properties, and methods, to manage Office's features.

What Is an Object?

Objects are the fundamental building blocks of Visual Basic; nearly everything you do in Visual Basic involves modifying objects. When you write Visual Basic macros for Office, your first task is to learn about the objects Office uses to represent its commands and features. For example, in Word the current, open document is stored in the Document object, and within each document is a Paragraph object corresponding to each paragraph.

A collection is an object that contains several other objects, usually of the same type. For example, the Documents object contains all the documents that are currently open in Word, and the Paragraphs object contains all the paragraphs in the current document or selection, as illustrated in Figure 38-2. By using properties and methods, you can modify a single object or an entire collection of objects.

Figure 38-2. This illustration shows a document object and paragraph collection.

What Are Properties and Methods?

Properties and methods are special keywords you use to manipulate Visual Basic objects. Using a bicycle metaphor, properties are attributes like the color or style of a bicycle (mountain bike, touring, or tandem), and methods are the actions a bicycle can perform (peddling, jumping, or coasting). Here are some more specific examples.

Properties Are Attributes

A property is an attribute of an object or an aspect of its behavior. For example, the properties of a document include its name, its content, its save status, and which windows are currently open to view it. To change the characteristics of an object, you change the values of its properties.

Here's how it works. To set the value of a property, write a program statement that contains the following elements, in this order: a reference to one or more objects, each followed by a period; the property name; an equal sign; and the new property value. For example, this program statement uses the SplitSpecial property to open a separate pane in the active window to display all the footnotes in a document:

 ActiveWindow.View.SplitSpecial = wdPaneFootnotes 

In this case, both ActiveWindow and View are objects. (The View object is contained in the ActiveWindow object.) SplitSpecial is a property that can be assigned one of 18 values associated with split windows. In this example, the value we assigned to SplitSpecial is a constant named wdPaneFootnotes, a special value used to identify footnotes in Word's object library. (You'll learn more about properties and constants in the next section.)

Methods Perform Actions

A method is an action that an object can perform. For example, you can use the Save method to save all the open documents in Word by writing the following program statement:

 Documents.Save 

In most cases, methods are actions, and properties are characteristics. Using a method causes something to happen to an object, while using a property returns information about the object or causes a quality of the object to change.

Learning More About the Office Object Model

Each chapter in Part 8 explores a new aspect of the Office object model, so you'll be getting lots of practice using objects, properties, and methods to streamline your document management tasks. However, you can also learn about the object model for each Office application on your own by using these features:

The Interactive Object Chart If you use the Word Visual Basic Office Assistant to search for "Microsoft Word Objects" in the online Help, Word will display an interactive chart that you can use to explore the programmable objects in Word. When you click an object in the chart, Word displays an online Help file that describes how you use the object in Visual Basic code. (The process is similar for each Office application.)

Object Browser The Visual Basic Editor includes a tool called the Object Browser that lets you display the properties and methods associated with all the objects in your system, including those supported by Office applications.

Microsoft Visual Basic online Help Office lists each object, property, and method in its online Visual Basic Help file for each application. You can access these Help topics by clicking the Office Assistant in the Visual Basic development environment. You can also press F1 while in the Object Browser or the Code window.

Code window Auto List Feature When you type the name of an Office application object and a period in the Code window, the Visual Basic Editor automatically lists all the properties and methods that you can use with the object. For example, if you type ActiveDocument and a period, a drop-down list of all the methods and properties associated with the ActiveDocument object appears. To specify one of the elements in your program code, simply click the desired method or property, and the element is appended to the statement.

In the following chapters, you'll learn more about using Office objects, properties, and methods in program statements. First, you'll learn how to assign values to properties by using Office constants.



Running Microsoft Office 2000 Small Business
Running Microsoft Office 2000
ISBN: 1572319585
EAN: 2147483647
Year: 2005
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net