上一篇:树目录菜单的制作 >>
用ASP创建Word文件
By Gardiner B. Jones
Background
BuildDoc.asp is an 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 use of 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. That is, what is shown in the form may
change based on user interaction (see the sample input form below).
The business need filled by BuildDoc is to enable sales associates to
create form letters from the changed records of a Web page table. Only
the data modified by the sales person is sent to Word, where it is fo
rmatted into a table. Obviously, all samples here are fictitious.
BuildDoc will 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 (see th
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 that has a row for each modif
ied row from the Web page form.
How To Do 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 will assume that all variables have
been first declared before use.
The code for the loading of the input form fields into 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" 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>
We create an instance of the Word Document object, using the sample co
de immediately below. Note that in Internet Explorer 4+ this will fail
unless the browser security is 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 that it is the same size as the number of
rows that are contained in the Web page‘’s form. In this case, we set
the Y-axis to a constant value of four because that 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 ready to examine all of the form rows. We do this by loopin
g through all 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 into, and then we put it there. The SELECT CASE stat
ement in the code sample below is important. It is where we determine
in which column the form field belongs. We used hard coded CASE option
s here for expediency.
<%
For intCount = 0 to frmData.fieldCount.value
strOkay = "Y"
strSearch = frmData.elements(intCount).name ‘’load the field name
strValue = frmData.elements(intCount).value ‘’load the field value
strPosition = Instr(1,strSearch,"_") ‘’get pos val of "_"
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
%>
Now we are ready to begin creating the document. We start by setting t
he Microsoft Word Document object RANGE using our variable, rngCurrent
, to the active document (just in case the user has a different docum
ent also open). Then we specify the table size by specifying its locat
ion ( rngCurrent ) and the number of rows and columns it needs.
<%
Set rngCurrent = objWordDoc.Application.ActiveDocument.Content
Set tabCurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add
rngCurrent,intNumrows,4)
%>
Having created the document with the table, we now begin populating th
e table with data. First we point to the first row ( tabRow=1 ), then
begin a loop that will run through each row. We insert a line feed [ C
hr(10) ] at the end of each row to put some white space between rows.
Finally, we increment our row counter, output the dollar values with "
FormatCurrency" to ensure use of dollar signs, commas and decimal plac
es. Right justification of dollar amounts is handled by setting the co
lumn in question to " ParagraphAlignment=2 ". I won‘’t tell you how muc
h of a pain it was to discover how to do that! Suffice it to say that
it is easier and better documented in VBA, which is not at all like wh
at is required in VBScript.
<%
For j = 1 to intTableRows
ObjWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Borders.E
nable=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.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(3).
Range.InsertAfter FormatCurrency(theArray(3,j))
objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(4).
Range.InsertAfter theArray(4,j)
objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(4).
Range.InsertAfter Chr(10)
objWordDoc.Applicatoin.ActiveDocument.Tables(1).Rows(tabRow).Cells(3).
Range.ParagraphFormat.alignment=2
tabRow = tabRow + 1
Next
%>
Finally, we finish up our document with some closing text and specifyi
ng the template‘’s location, and then our subroutine.
<%
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(
"Thank you for shopping at Acme Co., and please come again!")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(
" ")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(
" ")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(
"Regards,")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(
" ")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(
"Daryl B. Morticum")
objWordDoc.Application.ActiveDocument.Paragraph.Add.Range.InsertAfter(
"Sales Associate")
End Sub
%>
Hopefully this will get your gears spinning for ways you can do someth
ing similar. We are sure that we aren‘’t the only people who have had a
need to create a document from a Web page‘’s form. This is how we did
it. If you have a better way, or an improvement on or method, we would
love to hear from you.
下一篇:ASP翻页的实现方法 >>
相关文章:
- · asp提供在线文章翻译的功能
- · 关于Request
- · 基于B/S下的IC智能卡的安全问题
- · ASP小偷偷取地址并生成html
- · 求助关于远程访问ACCESS数据库的问题
- · Forms验证中的roles
- · 一个比较精辟的索引文章
- · T-SQL: 17 个与日期时间相关的自定义函数(UDF)
- · 限制某段IP地址
- · 调用winrar压缩文件夹
- · ASP做的剪包锤游戏
- · 如何在ASP+中使用自定义的PAGELET
- · 利用ASP编写动态回复表单
- · 用ASP和javascript实现网页上的动态分级目录
- · 在Windows 2000中配置ASP开发环境
- · ASP数据库编程SQL常用技巧
- · 用ASP方式实现拥有动态伸缩层次列表的主页
- · ASP写入响应流的最高效率测试结果
- · ASP网页访问权的控制
- · 用ASP实现的2000年倒记时程序
- · 用ASP实现网上考试系统
- · 用ASP构筑主页聊天室
- · ASP万用分页程序
- · 用ASP制作张扬个性的调查系统
- · 用ASP建立一个简单的聊天室
- · 用ASP设计网站在线人数统计程序
- · 用ASP设计一个留言薄(下)
- · 用ASP设计一个留言薄(上)
- · 用ASP建立站内信息搜索系统
- · 用ASP编写网站流量统计系统
- · 用ASP设计收发文管理系统(2)
- · 用ASP设计收发文管理系统(1)
- · 用ASP解决域名登记查询
- · ASP中用好“小甜饼”
- · 用ASP+Access创建网站RSS格式内容摘要
- · 用ASP实现网上“五子棋”大赛
- · 用ASP开发基于浏览器的文档数据库管理软件
- · 通用分页的存储过程函数
