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:
Other People Are Reading
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."
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."
Open the Visual Basic Editor. Click on the "Developer" tab. Click "Visual Basic."
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.
Dim wdCurrentApp As Word.Application Set wdCurrentApp = GetObject(, "Word.Application") wdCurrentApp.ActiveDocument.SaveAs "C:\My Documents\VBExample.docx"
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.
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."
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.
Open the Visual Basic Editor. Click on "Tools" and then point your mouse pointer to "Macro." Click on "Visual Basic Editor."
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"
Click on the "Save" icon in the Visual Basic Editor to save the changes.
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."
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for