The magazine of the Melbourne PC User Group
Harnessing the Power of Visual Basic in Word
Mark Mulvany |
How many people use the powerful functionality of
Visual Basic in Word to create documents in an interactive way? Indeed many users of Word do not
even know that Visual Basic comes with Word.
For some time I have been using Visual Basic in Word to assist me in my practice as a litigation
lawyer. It enables me to quickly format documents needed for filing with Courts.
One accesses the Visual Basic editor by pressing Alt+F11 in the normal document screen (Figure 1).
Much of the same Visual Basic functionality of the stand-alone versions of Visual Basic is
obtainable by using Visual Basic in Word. |

Figure 1. Press Alt+F11 to open the Visual Basic editor
|
I have found that one of the best ways to efficiently reproduce
real life aspects of civil litigation in code is by creating "User Defined Types". As one
illustration of this, a person who swears a Court affidavit is called a "deponent". Predictably a
deponent has a name, address, city or suburb, state and occupation. A User Defined Type for a
deponent could be as follows:
Public Type Dep
FullName As String
StreetAddress As String
CitySuburb As String
State As String
Occupation As String
End Type |
This creates the model of a deponent in code that can be used to
create any number of deponents in the running of the program. The above lines are the Declaration
of the User Defined Type and are placed in the Declarations section of a general module. When the
program needs a deponent the following code will create one:
Private MyDeponent As Dep
The value of using User Defined Types is that the whole "cluster" of related variables can be
passed into a subroutine, filled in with information, and then passed to other subroutines and used
for different purposes - such as the formatting of documents. Visual Basic in Word allows one to go
further and create Objects fashioned on real world concepts. However, I have found that User
Defined Types are convenient for my own purposes.
The User Defined Type "MyDeponent" declared by the line of code above could first be passed to
part of the program that prompts the user to fill it in. See Listing 1.
|

Listing 1. Prompting the user to fill in details
|
The code "FillDep MyDeponent" would pass the MyDeponent Type into
this subroutine and result in the user being prompted successively for the details necessary to
complete the information relevant to the particular deponent. The deponent "MyDeponent" would
return to the main program with the details filled in.
The whole point of the exercise is to use the Type to format text in Word. One passes the
filled-in Type to a part of the program which will format a document. The snippet of code in
Listing 2 is from a formatting subroutine that takes a User Defined Type and uses it for
formatting.
|

Listing 2. Code takes a User Defined Type and uses it to format
text
|
The text output in the active document from this "chunk" of code,
when the program is run, and after entry of the information, will be something like:-
"I, JOHN SMITH, of 5 Last Street, Frankston, in the State of Victoria, Taxi-Driver, MAKE OATH AND
SAY as follows:-".
It is also possible, with two parameters, to pass into a subroutine two different kinds of User
Defined Types at once and have them exchange information!
I have even managed to store deponents between sessions in Word by writing them to storage files
using file I/O. The storage files that the code creates can actually be opened, viewed and edited
outside Word although the program itself runs in Word. The code in Listing 3 will store a deponent
when the deponent Type is passed in to it:
|

Listing 3. Storing deponent information into file
|
To store a deponent one codes "StoreDep MyDeponent". In line 2 of
Listing 3 the value of the name of the deponent is used as the name of the storage file. The idea
is to enable multiple deponents to be stored in different files using their names as the file
names.
The reading of the file contents back into the program is accomplished by the code in Listing
4:
|

Listing 4. Reading of the file contents back into the
program
|
It is essential to test whether a file exists before attempting to
open it. The code in Listing 5 tests a new name entered by the user to see if there is an existing
file for the name, and if so, proceeds to open the file using the code in Listing 4.
|

Listing 5. Testing for an existing file name
|
I have much useful code related to litigation and much of it is of
general application. The function in Listing 6 is from my litigation programs and tests whether a
name entered by the user, eg. the name of a party to a case, is the name of a company or
not:
The following code uses the function in Listing 6.
TheName = InputBox("Enter
the full name:")
If TestIfCompany(TheName) = 1
Then...
|

Listing 6. Applying a string matching test to the deponent
name.
|
If the result is 1 then the name entered is the name of a company.
The program proceeds to prompt the user to enter the Australian Company Number for the party whose
name was entered.
Whatever the application, your Visual Basic code can be as sophisticated as needed. It has the
power to elevate your usage of Word to an exciting new level and a little time spent learning can
be most rewarding.
Reprinted from the May 2001 issue of PC Update,
the magazine of Melbourne PC User Group, Australia
|