You are here:About>Computing & Technology>Visual Basic
About.comVisual Basic
VBA - The Word Working Partner
Using VBA with Microsoft Word
Microsoft Word Icon

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!

(ps ... As mentioned in the Introduction, this tutorial is based on Word 2000 and VBA 6 because that's what most of our readers are using.)

 More of this Feature
• Part 2: Automate Your Letter Writing
• Part 3: The AboutVBA.DOC Code Sample
• Part 4: Word Objects
• Part 5: The Automated Tutorial Review Program
• Part 6: Using Toolbox Components
 
 Join the Discussion
Create a brilliant VBA app?
Tell Us About It!
 
 Related Resources
• Segment One of the VBA Tutorial
• The About Visual Basic Subjects List
• Beginning Visual Basic
• Visual Basic 6
 
 From Other Guides
• About Desktop Publishing for MS Word
• About Business Software
 
 Elsewhere on the Web
• MS: Word Objects
• MS: Templates vs Add-In's
 

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. The completed document with all of the VBA programs is avalilable for download here.

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 Tools > Options > File Locations tab and see what the startup folder is. You can also use the C:\Program Files\Microsoft Office\Office\Startup folder." I know that. We're doing it this way to learn how to code Word VBA programs.

First, start Word and then go to Tools > Macro > Visual Basic Editor. Click the Module option under the Insert menu. Enter this code. (Note that long lines are continued in most examples here. You can enter these lines without the continuation if you want to.)

Public Sub AutoExec()
   MsgBox ("The Word 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.

Startup Folder Message Box

Click the OK button on the MessageBox and then close the Visual Basic Editor. Save the document as a document template 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 open any document again. 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.

Next page > Automate Your Letter Writing > Page 1, 2, 3, 4, 5, 6
From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.