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. (A detailed description of the Word object model is available at the Microsoft web site at this link.
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 one of the higher level objects, Document, to see how we might do this.
Intellisense
The first way programmers normally do this today is to use a feature built in to Microsoft development tools called Intellisense. When an object is used in a Visual Basic program, Intellisense automatically provides a list of properties and methods in the object.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
The code in the illustration shows a Document object being declared:
Dim mydoc As New Word.Document
You will usually work with copies of objects, not the object itself. Remember, the object is just a program. New Word.Document makes a copy of the Document object inside the Word object. Dim mydoc gives the copy the name mydoc.
(The keyword Dim might seem like a strange choice to you if you haven't seen it before. Many years ago in early versions of the BASIC programming language, it was short for "Dimension". But today, it's just the keyword that works to declare a variable name in your program.)
Intellisense shows you methods and properties when a "dot operator" (it's actually just the period character) is entered after an object name. The illustration shows three methods and four properties (out of a much larger list that are available) of the Document object. The Activate method is highlighted. (The icons in the left column are there as a quick way to tell what is a method and what is a property.)
The Activate method shown will make mydoc the "active" document. The active document is the one that you type into in Word.
There are lots of other "libraries" of objects available to you as a programmer! Just as a sample, you can use libraries for 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 object model.
Events
There's one more programming feature of objects called events. Visual Basic is described as an "event driven programming model" because most program code starts running as a result of some "event" happening.
An event is just what it sounds like. For example, clicking the mouse is an event. In the case of the Document object, some possible events are:
- Close Event
- New Event
- Open Event
The Open Event is shown (along with others for ThisDocument, a name created by Word VBA for the Document object that our VBA program is contained in).
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Event subroutines
Event subroutines are called and executed whenever the event occurs. The "Hello World" program, AboutVB1, is not and event subroutine because to execute it, we have to specifically ask the host environment (that is, Word) to run it. To see an event subroutine in action, just add a similar line of code to the Open event subroutine shown earlier. When the document is opened in Word, some text is added to it automatically.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
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 and remember all of this, this, .... stuff?" This is why even experienced professional programmers often start with the Macro Recorder. You need all the help you can get.
Object Browser
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 too many details. We've already seen the Macro Recorder as an example of one tool. The Object Browser is another. Earlier, I said that, "there are lots of other "libraries" of objects available to you as a programmer!" The Object Browser is a great way to see the members of those libraries.
As an example, a "reference" to the Excel library was added to our Word project and some of the members of that library that have "xml" as part of the name are shown below.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
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: Objects in General.

