1. Computing
Objects! (and Methods and Properties)
4 - The Word Object Model
 Lesson 2
Objects!

1 - The Word VBA Macro Reconsidered

2 - The Nature of OOP

3 - So What Are Methods and Properties?

4 - The Word Object Model

5 - Objects in General


One way to learn about all the objects, properties, and methods available to you as a programmer in Microsoft Word is to consider the Word Object Model. Click THIS LINK to see a map provided by Microsoft.

Browsing through this model can be one way to find the objects you need for your program. Microsoft provides example code to help you. Let's look at the very top level object Documents to see how we might do this. (Click on Documents on Microsoft's web page to see the diagram.)

Documents

We can see links for properties, methods, and (What's this? Something new!) events are documented. (More about events in just a few paragraphs.)

Microsoft has selected the Close method as an example in their documentation. Although this statement looks a little like our "Hello World" program, there are some significant differences.

Documents("Report.doc").Close SaveChanges:=wdDoNotSaveChanges

  • A parameter with the file name of the file to open is passed to the object.
  • wdDoNotSaveChanges is called a built-in constant value

(Note: There are literally thousands of built-in constants that have been defined by Microsoft and other vendors. The purpose is just to give you a recognizable variable name instead of a 'magic number' in the program. wdDoNotSaveChanges, for example, is simply the number 0. The opposite constant, wdSaveChanges is the number -1.)

Even this model doesn't completely reveal everything available in Word, however. For example, our old friend the Selection object isn't in this model. That's because Selection is global and not strictly a 'child' of any other object ... so they wouldn't know where to put it in a tree diagram like this. Not to worry! Microsoft provides documentation for Selection HERE.

And there are lots of other "libraries" of objects available to you as a programmer, too! Just as a sample, all of the other Microsoft VBA compatible applications in Office like the Excel objects, the Access objects, and so forth. Some of them are even bigger than the Word model.

And there are also "events" ... Visual Basic is described as an "event driven programming model" because most program code starts running as a result of some "event" happening. In the case of the top-level Documents object, the possible events are:

  • Close Event
  • New Event
  • Open Event

So ... the way this works is that whenever a document is closed, created new, or opened, Word checks to see if there is a program that should be started. If there is, Word executes the program automatically. We're going to start a program later in this course as the result of a Click event for a button that we create. So we'll learn more about event coding later in the course.

By now, your head might be swimming in the complexity of it all: "How am ever going to learn all of this, this, .... stuff?"

Now are you starting to understand why even experienced professional programmers often start with the Macro Recorder?

The general answer is that nobody remembers all of it. If you don't learn how to use the computer itself to give you the information you need, you really do drown in details. We've already seen the Macro Recorder as an example of one tool. The Object Browser is another. To introduce you to the Object Browser, consider the built-in constants that we mentioned earlier. There are literally thousands of them. If you want to see a list, here's what Microsoft recommends:

For a complete listing of intrinsic constants available through VBA and each of the Office applications, open the Object Browser, select the appropriate type library from the Projects/Library box, type the appropriate constant prefix in the Search Text box, and then click Search on the Object Browser toolbar.

The "appropriate type library" is Word and the "appropriate constant prefix" is wd. The Object Browser is a primary tool for VB programmers ... especially when you graduate to more complex systems.

Object Browser

There's one more thing that you really ought to know about objects before we get back to a real program. That's the "big picture" about objects. Let's say a few words about that in the next section:

5 - Objects in General

©2014 About.com. All rights reserved.