1. Home
  2. Computing & Technology
  3. Visual Basic
VBA - The Word Working Partner
Automate Your Letter Writing
 More of this Feature
• Part 1: Using VBA with Microsoft Word
• 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
 

There are lots of applications that are possible now that you know how to do this. For example, if you use Word mainly to write letters and only occasionally for something else, you could write an AutoExec program to ask whether to load a default "personal letter" or the generic Word environment.

Public Sub AutoExec()
   Dim MsgBoxResult
   Dim SaveFileName
   MsgBoxResult = _
      MsgBox("Click Yes to load the default letter" _
      & vbCrLf & "Click No to load default Word.", _
      vbYesNo, "Document Environment")
   If MsgBoxResult = vbYes Then
      Documents.Open _
         ("C:\DefaultLetter.doc")
         SaveFileName = _
            InputBox("Enter a name for the letter", _
            "Letter Name ?")
         If SaveFileName <> "" _
            Then ActiveDocument.SaveAs (SaveFileName)
   End If
End Sub

There's a lot to be improved in this program. For one thing, keeping the default letter document in the root directory isn't usually recommended. (I did it that way to keep the lines short.) Adding your unique personalizations is left as an excercise for the reader.

There are also a lot of other techniques that would to improve it. For one thing, AutoExec in the startup folder is a holdover from the old WordBasic (a "true" macro language rather than the increasingly Object Oriented VBA). VBA still supports a number of the old WordBasic commands and this is one of them.

Next page > The AboutVBA.DOC Code Sample > Page 1, 2, 3, 4, 5, 6

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.