Microsoft Word/Visual Basic Tutorial

Written by tiesha whatley
  • Share
  • Tweet
  • Share
  • Pin
  • Email
Microsoft Word/Visual Basic Tutorial
Visual Basic for Applications turns normal Word documents into dynamic self-contained programs. (ANSI image by DBX60 from Fotolia.com)

The Visual Basic programming language is Microsoft's version of the classic programming language "Basic." Microsoft also added the functionality of the Visual Basic language to the backend of its Microsoft Office suite. Visual Basic for Applications (VBA) is the programming shell that can work with Microsoft Word to create complex and dynamic documents. The programming code is input in the Visual Basic Editor and run using Microsoft Word macros. To start using VBA in Word, you should learn how to enable macros, the basic structure of a function or procedure and how to run the code when finished.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Enable macros. VBA code runs in the form of macros in Word. If the security level is not set to run macros, you won't be able to run the code. Click on the "Office" button and then "Word Options." Click on the "Trust Center" tab. Click the "Trust Center Settings" and then "Macro Settings." Click the setting for "Trust access to the VBA project object model." Click "OK."

  2. 2

    Show the "Developer" tab if you haven't already. Click on the "Office" button again and then "Word Options." Click on the "Popular" tab if it isn't already selected. Click on the "Show Developer tab in the Ribbon" option and then click "OK."

  3. 3

    Open the Visual Basic Editor. Click on the "Developer" tab. Click "Visual Basic."

  4. 4

    Add your VBA code. You can add user forms, modules and class modules, depending on what you want to do in VBA; the options are numerous. For example, you can use VBA to save the current document with a new file name. Add a new module to do this. Click on "Insert" and then "Module." Type in the code for the procedure.

    "Sub SaveAsDocument()

        Dim wdCurrentApp As Word.Application
    
        Set wdCurrentApp = GetObject(, "Word.Application")
    
        wdCurrentApp.ActiveDocument.SaveAs "C:\My Documents\VBExample.docx"
    

    End Sub"

    This is the basic structure of VBA procedures, modules and class modules. Some are smaller than this, and some are much more detailed. Click on "File" and then "Save" to save the changes.

  5. 5

    Run your procedures. Go back to the main document window in Word 2007. Click on the "Developer" tab and then "Macros." Click on the name of your procedure and then click "Run." For instance, to run the code from above, click on "SaveAsDocument" and then click on "Run."

  1. 1

    Enable macros. Click on the "Tools" menu item. Click on "Macro" and then "Security." Change the security level to "Medium" and then click "OK." Restart Microsoft Word. Click on "Enable Macros" when asked.

  2. 2

    Open the Visual Basic Editor. Click on "Tools" and then point your mouse pointer to "Macro." Click on "Visual Basic Editor."

  3. 3

    Click on the "Insert" menu item and add the item that you want to create; user form, module or class module. Type in the VBA code for what you want the procedure to do in Word. Here is an example procedure that creates a new Word document from a template:

    "Sub NewTempDoc ()

       Dim wdWordApp as Word.Application
    
       Set wdWordApp = GetObject(, "Word.Application")
    
       wdWordApp.Documents.Add Template:="Elegant Memo.dot"
    

    End Sub"

    Click on the "Save" icon in the Visual Basic Editor to save the changes.

  4. 4

    Run the procedure. Go back to the main document window. Click on "Tools," "Macro" and then "Macros." Click on the name of the procedure that you created and then click "Run." To run the procedure from above, you would click on "NewTempDoc" and then click "Run."

Tips and warnings

  • You can learn more about other methods and functions in VBA by accessing the "Help" system in Visual Basic Editor by clicking on "Help" in the menu bar and then clicking on "Microsoft Visual Basic Help."

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.