creatdoc.asp
<!DOCTYPE html PUBLIC "-//W3C/Dtd html 3.2 Final//EN">
<head>
<title>星河影動之建立Word檔案</title>
<META HTTP-EQUIV="Refresh" CONTENT="30;URL='orderForm.asp'">
</head>
<%
dotLocation="'servernamedirectory heTemplate.dot'"
intRowCount = Request.Form("rowCount")
' 初始化行計數.
%>
<body Language="VBScript" onLoad="creatdoc document.theForm,
<%=dotLocation%>,intRowCount>
<FORM NAME="theForm">
-----------------------------------------------------------------------------------------------------------------------------------
在body標記中對onLoad調用,調用creatdoc VB指令碼子程式,向它傳遞3個參數:頁面中表單的內容(所有的隱含域)、Word模板檔案的位置、從輸入表單中收到的行數.讀所有的輸入表單域,然後當頁面裝載後調用creatdoc子程式
<%
itemCount = 0
' 設定域計數器為0.
For Each Item in Request.Form
' 總計表格域.
itemCount = itemCount + 1
' 使用For..Next 迴圈.
%>
<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>
-------------------------------------------------------------------------------------------------------------------------------------
開始建立一個Word 檔案對象. 在Internet Explorer 4以上版本中,要將瀏覽器的安全性設定為Low或 Custom
<%
Set objWordDoc = CreateObject("Word.Document")
ObjWordDoc.Application.Documents.Add theTemplate, False
ObjWordDoc.Application.Visible=True
%>
-------------------------------------------------------------------------------------------------------------------------------------
調整數組的維數使它與網頁表單所包含的行數相同.將Y軸設為4個常量,這是輸出檔案中所需要的欄數,X軸包含從表單中接收的行數
<% Redim Preserve theArray(4,intTableRows) %>
-------------------------------------------------------------------------------------------------------------------------------------
開始檢查所有的表單行.在所有輸入的網頁表單域中迴圈,收集每個表單網域名稱及其相應的值.逐個檢查並將其放入相應的數列元素內.在此,我們使用了不確定編碼的CASE選擇
<%
For intCount = 0 to frmData.fieldCount.value
strOkay = "Y"
strSearch = frmData.elements(intCount).name
' 裝入表單網域名稱.
strValue = frmData.elements(intCount).value
' 裝入表單域值.
strPosition = Instr(1,strSearch,"_")
intStringLen=strPosition-1
If intStrLen > 0 Then
strLeft = Left(strSearch,intStringLen)
strRight = Right(strSearch,(Len(strSearch)-Len(strLeft)-1))
Select Case strLeft
' Select Case命令決定表單域屬於哪一列.
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
%>
-------------------------------------------------------------------------------------------------------------------------------------
開始建立檔案
<%
Set rngCurrent = objWordDoc.Application.ActiveDocument.Content
' 對於啟用的檔案,用變數rngCurrent設定Word檔案對象RANGE,這是為了防止使用者開啟另一個檔案.
Set tabCurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add
rngCurrent,intNumrows,4)
' 通過指定表格的位置( rngCurrent)以及行、列的數目來確定其大小.
%>
------------------------------------------------------------------------------------------------------------------------------------
嚮往表格裡裝入資料.
<%
For j = 1 to intTableRows
' 首先指到第一行row(tabRow=1), 逐行迴圈.在每行結尾處插入斷行符號[Chr(10)],以產生行間空行,使行計數器增加.
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.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(3).Range.InsertAfter
FormatCurrency(theArray(3,j))
' 用FormatCurrency 輸出美圓值以保證使用$符號,逗號,小數點的位置.
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
' 在ParagraphFormat. alignment=2處設定欄數來實現美圓數量的正確調整.
tabRow = tabRow + 1
Next
%>
------------------------------------------------------------------------------------------------------------------------------------
指定模板位置,結束子程式
<%
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
%>
------------------------------------------------------------------------------------------------------------------------------------