Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

Everything You Need to Get Started Coding in VBA Macro with Word 2007

106 15
< Continued from page 3

Programs and documents

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 it often really confuses first timers: the difference between the program and the document. This concept is foundational.

VBA programs have to be contained in a host file.


In Word, the host is the document. In our example, that's AboutVB1.docm. The program is actually saved inside the document.

For example, 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 Windows application, we would have a program and a form.

(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.
  1. 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.))
  2. You can run it from the Editor using the Run icon or Run menu.


  1. 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 the program from Word is fairly easy to do. Just select the macro after clicking the Macro icon under the View tab.

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.

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.
Source...

Leave A Reply

Your email address will not be published.