The Basics of VBA
You can automate many routine tasks with Microsoft Word VBA Macros (actually, Macros are Visual Basic Sub Procedures; there are also Visual Basic Function Procedures). Microsoft Office applications, such as Word, Excel, PowerPoint, Outlook, Access and Project share a common programming language called Visual Basic for Applications (VBA).
VBA is an object-orientated programming language because when you develop a VBA application you manipulate objects. An object can be anything within your Office documents or even the office program itself. Objects have properties that describe the objects characteristics and Methods which are actions that can be done to the object.
VBA Terminology
VBA |
Visual Basic for Applications |
Code |
The statements or code that make up the program. These follow a set of rules, called syntax that govern how the commands are formulated. |
Object |
An object is an element that has to be acted upon eg a document, a shape, a table or paragraph |
Property |
Properties describe the objects characteristics eg a text box could have a font property |
Methods |
Methods are actions that can be carried out on the object |
Procedure |
Procedures are groups for commands which perform an action or calculates a value (function procedure) |
Module |
Modules are a collection of procedures |
Project |
A project is a collection of modules |
Event |
A specific action that occurs on or with an object eg Clicking a button |
Using the Visual Basic Editor
To view the Visual Basic Editor you must show the Developer Tab on the Ribbon by selecting File, Options, Customize the Ribbon and on the right hand side check the box next to Developer
Visual Basic Editor
Word is supplied with the Visual Basic Editor for modifying and creating macros and for creating user-defined forms.
The Visual Basic Editor is common to all the Microsoft Office applications. To view the VBE select the Developer Tab, Code Group, Visual Basic or press ALT F11.
To start with, the Visual Basic Editor contains two important windows: the Project Explorer and the Code window.
Components of the Visual Basic Editor
Project Explorer
The Project Explorer displays a hierarchical list of the projects and folders with all of the items contained in and referenced by each of the projects.
Project
The name of an open document or template.
Forms
Any user-defined forms.
Modules
Recorded and written procedures (macros). To create a new module Select Insert, Module
Code Window
Use the Code window to write, display and edit Visual Basic code. Open as many Code windows as there are modules; view the code in different forms or modules and copy and paste between them.
Open a Code window from the Project window, by double-clicking a form or module.
Drag selected text to:
- A different location in the current Code window.
- Another Code window.
- The Immediate and Watch windows.
- The Recycle Bin.
Object Box
Displays the name of the selected object. Click the arrow to the right of the list box to display a list of all objects associated with a form.
Procedures/Events Box
Lists all the events recognised by Visual Basic for a form or control displayed in the Object box. When an event is selected, the event procedure associated with that event name is displayed in the Code window.
If (General) is displayed in the Object box, the Procedure box lists any declarations and all of the general procedures that have been created for the form. If editing module code, the Procedure box lists all of the general procedures in the module. In either case, the procedure selected in the Procedure box is displayed in the Code window.
All the procedures in a module appear in a single, scrollable list that is sorted alphabetically by name. Selecting a procedure using the drop down list boxes at the top of the Code window moves the cursor to the first line of code in the procedure selected.
Split Bar
Dragging the Split bar down, splits the Code window into two horizontal panes, each of which scrolls separately. View different parts of code at the same time. The information that appears in the Object box and Procedures/Events box applies to the code in the pane that has the focus. Dragging the bar to the top or the bottom of the window or double-clicking the bar closes a pane.
Margin Indicator Bar
A grey area on the left side of the Code window where margin indicators are displayed. Margin indicators provide visual cues during code editing.
Procedures
A procedure is a series of statements of Visual Basic code stored in a module of a Visual Basic project. When a macro is recorded Word writes a procedure. The code can be modified and special Visual Basic commands can be included to allow user interaction, conditional testing, looping and other options.
Each procedure is identified by a name that is written at the top of the procedure.
Sub Procedures
Sub procedures perform actions. A macro is a recorded Sub procedure.
A Sub procedure is enclosed by Sub and End Sub statements, the Sub statement contains the macro name:
Sub Macro1 ()
Commands
End Sub
The macro name is followed by brackets. The brackets may contain variable data, called arguments.
Private and Public Procedures
Public procedures are accessible to all other procedures in all modules. If used in a module that contains an Option Private statement, the procedure is not available outside the project.
Private procedures are accessible only to other procedures in the module where they are declared.
All procedures are public by default, except for event procedures. When Visual Basic creates an event procedure, the Private keyword is automatically inserted before the procedure declaration. For all other procedures, the procedure must be explicitly declared with the Private keyword if it is not to be public.
Inserting Comments
It is very important to put explanatory comments into a macro, as it will make the macro easier to read and “debug”.
Comment text can be placed in a line on its own or following a macro statement.
Comment text is prefixed by an apostrophe. The apostrophe tells Visual Basic to ignore the text that follows it and to continue execution with the statement on the next line.
Comment text normally appears in a green font, but this style can be altered.
Colour Coding
Descriptive text or VB documentation, to use its correct term, is preceded with an apostrophe (‘) and is shown in Green. Documentation can exist on its own line or to the right of other code lines, but always preceded with the apostrophe, otherwise VBA thinks that it is active code.
Keywords are colour blue. This includes Sub and End Sub, and enclosed routines such as If…Then and End If (for a branching routine), With and End With for a formatting routine and many others. Keep a look out for key words and refer to the exercises in this course –mostly, they start or end routines and procedures.
If you make an error in a code line, it MAY turn red. MAY, because the VB Editor is not intelligent enough to spot certain mistakes such as spelling mistakes. Punctuation however and key syntax points should be recognised. At least, if you get a line of code turn red, you know it contains an error.
All other code text is black. Tabs are often used to separate sections of code – use tabs at your own discretion to highlight parts of the macro.
Note: If a code line turns red an error message is displayed. If you Cancel the error message you are allowed to continue writing code on lines above or below. The incorrect line will remain red until de-bugged (fixed).
Find out more at http://www.eident.co.uk/training-courses/microsoft-office-training-courses/
For detailed information on Microsoft Word VBA visit http://www.visualbasictraining.co.uk/microsoft-word-visual-basic-for-applications/