日期:2009-07-14  浏览次数:21249 次

运行下列的脚本需要将IE的安全级别设为低,或在中级安全级别下将那个ActiveX控件设为启用,忘记了:

<HTML>
<head>

<title>Build Document by Script</TITLE>
</HEAD>
<Table id="myData" border=1 align=center>
<Tr align=center>
  <Td>产品名称</Td>
  <Td>产品描述</Td>
  <Td>产品单价</Td>
  <Td>产品等级</Td>
</Tr>
<Tr align=center>
  <Td>产品一</Td>
  <Td>This is a test</Td>
  <Td align=right>300.50</Td>
  <Td>一级</Td>
</Tr>
<Tr align=center>
  <Td>产品二</Td>
  <Td>This is a test</Td>
  <Td align=right>300.50</Td>
  <Td>一级</Td>
</Tr>
<Tr align=center>
  <Td>产品三</Td>
  <Td>This is a test</Td>
  <Td align=right>300.50</Td>
  <Td>一级</Td>
</Tr>
</Tabld>
<input type=button onclick="vbscript:buildDoc '',4" value="build">
<BODY>
</BODY>
</HTML>
<script language="vbscript">
Sub buildDoc(theTemplate,intTableRows)
  Dim Table1
  set Table1 = document.all.myData
  row = Table1.rows.length
  Set objwordDoc = CreateObject("word.Document")
  ObjwordDoc.Application.Visible=True
  Dim  theArray(4,4)
  'Redim Preserve theArray(4,row)
  colnum = Table1.rows(1).cells.length
  for i=0 to row-1
    for j=0 to colnum-1
      theArray(j+1,i+1) = Table1.rows(i).cells(j).innerHTML
    next
  next

  intNumrows = 4

  objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("测试的表格")
  objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")
  objwordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("")

  Set rngPara = objwordDoc.Application.ActiveDocument.Paragraphs(1).Range
  With rngPara
    .Bold = True
    .ParagraphFormat.Alignment = 1
    .Font.Name = "Arial"
    .Font.Size = 12
  End With

  Set rngCurrent = objwordDoc.Application.ActiveDocument.Paragraphs(3).Range
  Set tabCurrent =  ObjwordDoc.Application.ActiveDocument.Tables.Add(rngCurrent,intNumrows,4)

  for i = 1 to colnum
    objwordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.InsertAfter theArray(i,1)
    objwordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.ParagraphFormat.alignment=1
  next
  tabRow = 2

  For j = 2 to intNumrows
    '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.ActiveDocumen