word| Create |word Using Active Server Pages to build Microsoft word Documents
by Gardiner B. Jones
Background
Builddoc.asp is a Active Server Page (ASP) that reads the output of a
Web page form, and creates as output a Microsoft Word document Contai
Ning a table of changed data within the form. Forms are no longer limi
Ted to containing static information. With database connectivity, the
Increasing with Dynamic HTML (DHTML), and the growing interest in XM
L, it has become common practice in business Web pages for the data Co
Ntained in them to be dynamic. This is, what are shown in the form may
Change based on user interaction (the sample input form below).
The business need filled by Builddoc are to enable sales associates to
Create form letters from the changed records of a Web page table. Only
The data modified by's sent to Word, where it is fo
Rmatted into a table. Obviously, all samples here are fictitious.
Builddoc'll read all of the information on the form, identifying WHI
CH rows have been changed, and then creates the Microsoft Word documen
T using only the information contained within the changed rows (
E sample output document below). Builddoc uses a template file (buildd
Oc.dot) that contains the address header, and some preformatted text.
It then writes a table into the document which has a row for each modif
IED row from the Web page form.
How Todo It
We start by reading all of the Web page form fields into hidden form F
Ields on the receiving Web page. In the source code below, note the "
OnLoad "Call in the" body tag. It calls the Builddoc VBScript Subrouti
NE, passing three parameters to it:the contents of the page ' s form (a
ll the hidden fields), the location of the Word template file, and the
Number of rows received from the input form. The input form fields AR
e all read and then, when the page loads, it calls the Builddoc Subrou
Tine. For the sake of brevity, we'll assume that all variables have
Been-declared before use.
The code for the loading of the input form fields to builddoc.asp is
thus:-
<! DOCTYPE HTML PUBLIC "-//w3c/dtd HTML 3.2 final//en" >
<HEAD>
<title>build document</title>
<meta http-equiv= "Refresh" content= "30; Url= ' orderform.asp ' ">
</HEAD>
<%
dotlocation= "' \\servername\directory\theTemplate.dot '"
Introwcount = Request.Form ("rowcount") ' Initialize a row counter
%>
<body language= "VBScript" Theform ">
<%
ItemCount = 0 ' Set field counter to zero
For each Item in Request.Form ' Count up the Form fields
ItemCount = itemCount + 1 ' using for.. Next Loop
%>
<input type= "hidden" name= "<%=Item%>" value= "<%=request (Item)%>" >
<% Next%>
<input type= "hidden" name= "Numbrows" value= "<%=intRowCount%>" >
<input type= "hidden" name= "FieldCount" value= "<%=itemCount%>" >
</FORM>
</BODY>
</HTML>
We create an instance of the Word Document object, using the sample Co
De immediately below. Note So in Internet Explorer 4+ this'll fail
Unless the browser security are set to low, or Custom with the APPROPR
Iate setting to run programs.
<%
Set Objworddoc = CreateObject ("Word.Document")
ObjWordDoc.Application.Documents.Add Thetemplate, False
Objworddoc.application.visible=true
%>
We re-dimension Our array so this it is the same size as the number of
Rows that are contained the Web page ' s form. In the case, we set
The y-axis to a constant value of four because this is the number of C
Olumns we need in the output document. The x-axis contains the number
of received rows from the form.
<%
Redim Preserve TheArray (4,inttablerows)
%>
Now we are are ready to examine all of the form rows. We do this by Loopin
G through all of the Input Web page form fields to collect each form Fiel
d name and corresponding value. We test each to determine which array
element to put it in, and then we put it there. The SELECT case stat
Ement in the code sample