Create and Modify MS Word Templates

The possibility to create Word documents based upon a template and the data from the application gives the user an easy way to define documents that can be generated.

The available information that may be used in the document consists of a fixed number of fields that can be inserted inside the document text. There are 51 items defined and supplied in an employeedata.htm file that comes with the application. The file currently includes the following database items:

Company Information

Company Name, Association No, Company Address (all fields due to the company address presentation set up), Company Address1, Company Address2, Company Zip Code, Company City, Company State, Company County, Company Country, Company Phone, Company Fax, Company Email

Employees Personal Information

First Name, Last Name, Address, Address1, Address2, Zip Code, City, State, County, Country, Gender, Date of Birth, Place of Birth

Employment Related Information

Emp No, Date of Employment, Date of Leaving, Employment Comments, Work Time, Leaving Cause, Date Start Position, Date End Position, Position Title, Job Title, Manager Name, Manager Job Title, Salary per Year, Salary per Month, Salary per Hour, Salary Amount, Salary Period, Salary Pay Frequency, Salary Currency Code

Set Up

The basic data is set up in the Word Template Definition window. In this window, the document name including the file path is entered together with a description. Other columns determine where the document is used and what data can it fetch from the employeedata.htm file mentioned above. If no file path is entered, the application will look for the document in its runtime folder.

Generating Documents

Below is an example of a document template.

In order to generate this document for a specific employee you:

  1. Open the Employee window.
  2. Select an employee.
  3. Select 'Generate Document...' from the right mouse button menu.
  4. Select the appropriate document and press OK.
  5. Answer any questions that appear (varies between templates).

The result is a MS Word document.

Creating document templates

Overview

To create a new document template, the user needs to do the following:

Create and Prepare Document

To create a document template, open the Microsoft Word and create a new document. Once a document is created you will have to access the Mail Merge options. Depending on the version of Microsoft Word, Mail Merge is accessible from different locations.

Mail Merge should be started for the current/active document (the document you have just created) and have form of a Letter (Form Letters in older Word versions). For a data source, select the employeedata.htm file in the installation directory (runtime folder).

For more information about performing a mail merge, please consult the documentation relevant for the Microsof Word version that you are currently using.

Add text and Mail Merge Objects

To insert an field that will fetch information from the application, use the Insert Merge Field option. Clicking on it will allow you to select what information you want to appear in the merge field (e.g., a company address, Job Title, First and Last names).

Through selection of the appropriate fields that will appear in the running text, a template is created.

 

 

Add Visual Basic Macro

For the document template to work, a Visual Basic macro has to be added. Depending on the version of Microsoft Word, there are various ways of accessing the macro edition. The macro menu can be accessed by pressing alt + F11 keys. For more information on macros, please read Microsoft documentation for the relevant Word version. In some versions, macro edition field for the document will appear automatically, and in others a relevant document has to be manually selected from the graphical document structure (a "tree" structure).

In the macro edition, add the following code (just copy the code and paste it in the code window):

Public Sub DoMailMerge(sFile As String)

    ActiveDocument.MailMerge.OpenDataSource Name:=sFile, _ 
        ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _ 
        AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _ 
        WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _ 
        Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1 _ 
        :=""
    With ActiveDocument.MailMerge 
        .Destination = wdSendToNewDocument 
        .MailAsAttachment = False 
        .MailAddressFieldName = "" 
        .MailSubject = "" 
        .SuppressBlankLines = True 
        With .DataSource 
            .FirstRecord = wdDefaultFirstRecord 
            .LastRecord = wdDefaultLastRecord 
        End With 
        .Execute Pause:=False 
    End With 
End Sub 

Add to client and test

To test the newly ceated document, add it in the Word Template Definition window, select an appropiate document type and select what data should be included. Then, you can test the document by opening a relevant window (e.g., Employee), right clicking the header and clicking Generate Document. In a dialog box that will appear, a document should be available for selection. Once you selected it and pressed OK, document should appear with all the relevant information entered.