Creating a Document Management System in Word
Overview Of UserFormDMS Form Code
The main code being run is tied to the OKAY button. Below is a listing of the code
and a brief explanation of how it operates.
Private Sub CmdOpenUserFormDMS_Click()
Dim oCheck As MSForms.Control
Dim x As Long
For x = 1 To 5
Set oCheck = Me.Controls("OptButton" & x)
If oCheck.Value = True Then Documents.Add Template:= _
"D:\Stuff\Web Page Stuff\" & oCheck.Caption, _
NewTemplate:=False
End If
Next x
Unload UserFormDMS
End Sub
The first line of the code is the name of the macro. Every macro starts with a “Sub” and
ends with an “End Sub”. The lines in between are the real code.
Basically this code declares a variable “oCheck” for all of the option buttons (radio buttons)
on the form. This sample only has one button on each TAB (total of 5), but you can have as
many as you can fit. The next line declares a variable for the total number of buttons (in
this case 5).
The next set of lines (from the “For x = 1 to 5” through “Next x” looks to see which button
was clicked. Based on the property name of the button, it goes to the desired folder (could be
a network folder) and opens the file that has the same name as the caption property of that
button. This brings to light a very important part - The caption property of each button MUST
be exactly the same as the file name! That is why it is a good idea to give a reasonable
descriptive name of your file (one long enough to make sense, but not too long that the caption
property is too big for the form). In the example above, “OptButton1” on the first TAB has a
Caption property of “Employee Expense Form”. The actual file name in the respective folder is
“Employee Expense Form.dot”. Keep in mind that this code is designed to create a new document
from an existing template. It is NOT designed to open documents. We will visit some of these
Form design specifics below.
The last thing the code does is to unload (or close out) the UserFormDMS. We will show you
later how easy it will be to access this form by putting a button on a toolbar that is accessed
every time you open Word.
Even though the code might look somewhat confusing, all you will need to do to modify the
code to fit your specific needs are:
- Change the “5” in the “For x = 1 to 5” to the number of option buttons (forms) you will have.
- Change the path ("D:\Stuff\Web Page Stuff\") to the path of the templates you are using.
The only other thing you need to do is to create new option buttons on the desired TAB.
This next section will provide those instructions.
Download the White Paper (Opens PDF version of WhitePaper in new window)
|