Create a Microsoft Word file with ASP

Source: Internet
Author: User
Tags range first row
Word| creation with the increasing interest in database and Dynamic HTML (DHTML) and XML, it is becoming more and more common to use dynamic content in the data of commercial web sites. Here we have successfully used a new technique--Create Word files with ASP, I think you will be greatly inspired by the look.
Original source: http://www.asptoday.com/articles/19990825.htm

Background

Builddoc.asp is an ASP file that reads the output of a Web page form and creates a Microsoft Word file as an output that contains a table based on changes in the data in the form. The content of the form is no longer limited to static information. That is, the content displayed in the form may change as the user interacts.




  
The business requirement for Builddoc is to create a form letter based on the record of the Sales person's page list. Only data that has been modified by the salesperson is sent to Word, where the data is formatted into the table.

Builddoc reads all the information on the form, identifies the changed rows, and then creates a Microsoft Word file with the information contained in the changed row. Builddoc uses a template file (Builddoc.dot) that contains the address header and some preformatted text. It then writes a table to the file, each of which corresponds to a row that has been modified in the Web page's form.




  
How do you do it?

At the beginning, all of the page form fields are read into the hidden form field of the received Web page. In the following source code, notice the call to "OnLoad" in the body tag. It calls the Builddoc VB Script program, passing it 3 parameters: the contents of the form in the page (all suppressed fields), the location of the Word template file, the number of rows received from the input form. Read all the input form fields, and then call the Builddoc subroutine when the page is loaded. For brevity, it is assumed that all variables are declared before they are used:

The code for loading input form fields in Builddoc.asp is as follows:


〈! 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= "' Servernamedirectory Hetemplate.dot '"
Introwcount = Request.Form ("rowcount") ' Initialize a row counter
%>
〈body language= "VBScript" onload= "Builddoc Document.theform,
〈%=dotlocation%>,introwcount>
〈form name= "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>
Use the code in the following example to create a Word file object. Note that in Internet Explorer 4+, the security of the browser is set to low or Custom to enable the application to run successfully.


〈%
Set Objworddoc = CreateObject ("Word.Document")
ObjWordDoc.Application.Documents.Add Thetemplate, False
Objworddoc.application.visible=true
%>
Adjusts the dimensions of the array so that it is the same number of rows as the Web page form contains. At this point, set the Y-axis to 4 constants, which is the number of columns required in the output file. The x axis contains the number of rows received from the form.

〈% Redim Preserve thearray (4,inttablerows)%>

Now start checking all the form lines. Loop through all the entered page form fields, collecting each form's domain name and its corresponding value. Check each to decide which sequence element to put into it, and then put it in. The Select Case command in the following example code is important, which determines which column the form field belongs to. For convenience, use an indeterminate code for case selection.


〈%
For intcount = 0 to FrmData.fieldCount.value
Strokay = "Y"
Strsearch = frmdata.elements (intcount). Name ' Load ' field name
strvalue = frmdata.elements (intcount). Value ' Load the field value
Strposition = Instr (1,strsearch, "_") ' Get pos val ' _ '
Intstringlen=strposition-1
If Intstrlen > 0 Then
Strleft = Left (Strsearch,intstringlen)
Strright = Right (Strsearch, (Len (Strsearch)-len (strleft)-1))
Select Case Strleft
Case "SKU" intarrayy=0
Case "description" Intarrayy=1
Case "Price" intarrayy=2
Case "Quantity" intarrayy=3
End Select
Intarrayx = Strright
If strokay〈> "N" Then
TheArray (Intarrayy, intarrayx) = strvalue
End If
End If
Next
%>
Start creating the file now. For an activated file, set the Microsoft Word file range (to prevent the user from opening another file) by using the variable rngcurrent to determine its size by specifying the table's location (rngcurrent) and the number of rows and columns.


〈%
Set rngcurrent = objWordDoc.Application.ActiveDocument.Content
Set tabcurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add
rngcurrent,intnumrows,4)
%>
After creating the file with the table, we started loading the data into the table. Start by pointing to the first row of row (tabrow=1), followed by a line-by-row loop. Inserts a carriage return at the end of each line [CHR (10)] to produce a blank line between the rows, the last increment of the line counter, and the "formatcurrency" output dollar value to guarantee the use of the $ symbol, comma, and decimal point position. Through the
"Paragraphalignment=2" Set the number of columns to achieve the correct adjustment of the U.S. dollar number. It's easier to use VBA, not as difficult as in VBScript.


〈%
For j = 1 to Inttablerows

ObjWordDoc.Application.ActiveDocument.Tables (1). Rows (Tabrow). Borders.enable=false

ObjWordDoc.Application.ActiveDocument.Tables (1). Rows (Tabrow). Cells (1). Range.InsertAfter
TheArray (1,J)

ObjWordDoc.Application.ActiveDocument.Tables (1). Rows (Tabrow). Cells (2). Range.InsertAfter
TheArray (2,J)

Objworddoc.appli



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.