We have our glorious and complicated system ... consisting of one program statement ... but now we want to run it. Here's what that's all about.
There's one concept to be learned here that is very important and can really confuse first timers ... the difference between the PROGRAM and the DOCUMENT. This concept is foundational and is part of programming as far up the ladder as you want to go.
But the names change.
If this was Excel, we would be talking about the PROGRAM and the SPREADSHEET. In Access, the PROGRAM and the DATABASE. Even in standalone Visual Basic, we would probably have a PROGRAM and a FORM. Visual Basic programs that really are standalone ... that is, there is no other containing object ... do exist. And they even become more important parts of a system in more advanced programming. But in VBA, and in fact in most day-to-day programming these days, there is an object that works alongside your program code. We'll switch between them very deliberately in this section of the course to make it clear.
(Note: There is a trend in programming to refer to all high level containers as a "Document". This is specifically the case when XML ... another up and coming technology ... is being used. Don't let it confuse you. Although it's a slight inaccuracy, you can think of "documents" as being roughly the same as "files".)
There are ... ummmmm .... about three main ways to run your VBA macro.
- You can run it from the Word Document.
(Note: Two subcategories are to select Macros from the Tools menu or just press Alt-F8. If you have assigned the macro to a Toolbar or Keyboard shortcut, that's one more way.))
- You can run it from the Editor using the Run icon or Run menu.
- You can single-step through the program in debug mode.
You should try every one of these methods just to become comfortable with the Word/VBA interface. When you finish, you will have a whole document filled with repeats of Hello World!
Running it from Word is fairly easy to do. No more explanation is really needed.
To run it from the Editor, first open the Visual Basic editor and then either click the Run icon or select Run from the menu. Here's where the difference between the Document and the Program might become confusing to some. If you have the document minimized or perhaps have your windows arranged so the editor is covering it, you can click the Run icon over and over and nothing seems to happen. But the program is running! Switch to the document again and see. When you're looking for a problem in a program (this is called 'debugging') that acts on the contents of a document, a valuable technique is to check the document as you run the program to get some insight into just what the program is doing.

Single stepping through the program is probably the most useful problem solving technique. This is also done from the Visual Basic editor. To try this out, press F8 or select Step Into from the Debug menu. The first statement in the program, the Sub statement, is highlighted. Pressing F8 executes the program statements one at a time until the program ends. You can see exactly when the text is added to the document this way.
There are a lot of more refined debugging techniques such as 'Breakpoints', examining program objects in the 'Immediate Window', and the use of the 'Watch Window'. But for now, simply be aware that this is a primary debugging technique you will use as a programmer.

Of course, everything has gone so well that you can probably just skip the next section. But for those who might have had a wee bit of trouble, we present:
5 - What Could Go Wrong?
You can browse questions and answers about problems that might come up. Or you can forge ahead to the next topic:
6 - OK ... Now What's Next?