Microsoft Word is an amazingly complete and complex environment to work in. Most of us wordsmiths (and I count myself as one since I write a lot) don't use even a part of the capability that is built into Word. So it's natural to ask the question, "Why do I need even more capability in VBA programs that I write myself?"
Read On and See!
(This tutorial is based on Word 2007.)
No matter how many features Microsoft builds into Word, there are going to be things that you will want to do over and over in lots of documents. There may also be functions in other documents, like part of an Excel spreadsheet, that you want to use in your Word document. And ... Microsoft may have simply left out a feature that you want to have available in your "personalized" version of Word.
To introduce you to Microsoft Word VBA programming, we're first going to code a very short program that is sort of a "bootstrap" for learning to program VBA. After that's done, we'll create a few more short VBA programs to illustrate components, and provide additional code samples.
We're going to create a program that starts automatically and displays the path to the Word "startup folder". In order to do this, we're going to have to find out what the startup folder is. Before you say, "Hey! You can just go to Office Button > Word Options > Advanced > File Locations tab and see what the startup folder is. It's probably C:\Users\myComputer\AppData\Roaming\Microsoft\Word\STARTUP anyway."
We're doing it this way to learn how to code Word VBA programs.
First, start Word 2007. Look for the "Developer" tab. It's not turned on by default so if it's not visible, click the Microsoft Office Button, and then click Word Options. Click Popular, and select the Show Developer tab in the Ribbon check box. That should add a new "Developer" tab to your system. Click that tab to bring up the Visual Basic development environment and then click the Visual Basic icon to launch the VB editor.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Click the Module option under the Insert menu. Enter this code. (Note that long lines are broken into shorter lines using the "_" continuation character in most examples here. You can enter these lines without the continuation if you want to.)
Public Sub AutoExec()
MsgBox ("The Word 2007 Startup Folder is: " _
& vbCrLf & Word.Application.StartupPath)
End Sub
Press F5 to display the list of macros in your document and select AutoExec. Then click Run. You should see a MessageBox display something like this.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Click the OK button on the MessageBox and then close the Visual Basic Editor. Save the document as a "Word macro enabled template" document type in the folder that you have now learned (from the message in the MessageBox) is the startup folder. It doesn't matter what you name the document template as long as it is in the startup folder. Now, close Word and restart it. This time, you should see the MessageBox displayed as soon as Word opens.
You probably don't want this message to be displayed whenever you open Word from this point on. To stop it, just delete the document template you saved earlier from your startup folder.
But you want to do more than just figure out what the Word startup folder is. On the next page, we write a really simple, but still useful program.
