Understanding what a VBA program is
If you have closed Word, open it again and select the AboutVB1.docm file that you saved in the previous lesson. If everything was done correctly, you should see a banner at the top of your document window with a security warning.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
VBA and Security
VBA is a real programming language. That means that VBA can do just about anything you need it to do. And that, in turn, means that if you receive a Word document with an embedded macro from some 'bad guy' that macro can do just about anything too. So Microsoft's warning is to be taken seriously. On the other hand, you wrote this macro and all it does is type "Hello World" so there's no risk here. Click the button to enable macros.
To see what the Macro Recorder has created (as well as to do most other things that involve VBA), you need to start the Visual Basic Editor. There's an icon to do that at the left side of the Developer ribbon.
First, notice the left hand window. This is called the Project Explorer and it groups together the high level objects (we'll talk more about them) that are part of your Visual Basic project.
When the Macro Recorder was started, you had a choice of the Normal template or the current document as a location for your macro. If you selected Normal, then the NewMacros module will be part of the Normal branch of the Project Explorer display. (You were supposed to select the current document. If you did select Normal, I recommend that you delete the document and repeat the previous instructions.) Select NewMacros under Modules in your current project. If there still isn't any code window displayed, click Code under the View menu.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
The Word document as a VBA container
Every Visual Basic program must be in some kind of file 'container'. In the case of Word 2007 VBA macros, that container is a ('.docm') Word document. Word VBA programs can't run without Word and you can't create standalone ('.exe') Visual Basic programs like you can with Visual Basic 6 or Visual Basic .NET. But that still leaves a whole world of things you can do.
Your first program is certainly short and sweet, but it will serve to introduce the major features of VBA and the Visual Basic Editor.
The program source will normally consist of a series of subroutines. When you graduate to more advanced programming, you'll discover that other things can be part of the program besides subroutines.
This particular subroutine is named AboutVB1. The subroutine header must be paired with an End Sub at the bottom. The parenthesis can hold a parameter list consisting of values being passed to the subroutine. Nothing is being passed here, but they have to be there in the Sub statement anyway. Later, when we run the macro, we will look for the name AboutVB1.
There is only one actual program statement in the subroutine:
Selection.TypeText Text:="Hello World!"
Objects, methods and properties
This statement contains the big three:
- an object
- a method
- a property
The statement actually adds the text "Hello World." to the contents of the current document.
The next task is to run our program a few times. Just like buying a car, it's a good idea to drive it around for a while until it feels a little bit comfortable. We do that next.
