透過Word97印表格

來源:cww

在VB中,印報表經常交由Crystal Report來做,當然,前提是資料庫的資料,如果不透
過Crystal Report,那也得用Printer物件來做,那就做苦功了,尤其是表格的印表,
表格複雜者,不用多說,畫起來不吐血才怪,所以啦,使用Office 97中,Word來做,
可能是另一個選擇。Office 97本身是一個ActiveX Server,所以,我們可以透過VB來
要求Office 97來完成這些事,即VBA的做法。

首先,我們要使用Word 97把所需的表格造出來,裡面剩下資料的部份,再VB透過VBA
的方式填進來,好比用Word 97做套版,vb填資料。而word97提供了錄製巨集的功能,
這個功能記錄了Word的動作,將之變成ActiveX Server的function Call,將這些碼,
Copy到VB裡面,就可以使用了,所以啦,想做某個功能,卻苦於Word ActiveX Server
物件、方法之複雜時,使用錄製巨集來幫助我們。

在VB的部份,先在設定使用項目中選Microsoft Word8.0 Object Library,而後如程
式中的說明。假設test.doc的報表格式如下

這是一個報表測試程式,您也試試
+--------+------------+------------+-----------+-----------+
|          This is a testing  Report                       |
+--------+------------+------------+-----------+-----------+
|        | title1     |  title2    |  title3   | title4    |
+--------+------------+------------+-----------+-----------+
| fld1   |            |            |           |           |
+--------+------------+------------+-----------+-----------+
| fld2   |            |            |           |           |
+--------+------------+------------+-----------+-----------+

執行完VB後的newfile如下
這是一個報表測試程式,您也試試
+--------+------------+------------+-----------+-----------+
|          This is a testing  Report                       |
+--------+------------+------------+-----------+-----------+
|        | title1     |  title2    |  title3   | title4    |
+--------+------------+------------+-----------+-----------+
| fld1   |            |            |           |           |
+--------+------------+------------+-----------+-----------+
| fld2   | 第一格     |  第二格    |  第三格   |  第四格   |
+--------+------------+------------+-----------+-----------+
|NewLine |            |            |           |           |
|2'line  |            |            |           |           |
+--------+------------+------------+-----------+-----------+

Option Explicit
Private wd As Word.Application
'Dim wd As Object '如果沒有Word8.0 Object Lib則改用這行
Dim ww As Document
Private Sub Command1_Click()

Set wd = New Word.Application  '產生一個Word執行個體
'Set wd = CreateObject("Word.Application") '如果沒有Word8.0 Object Lib則改用這行
Set ww = wd.Documents.Open("c:\My Documents\test.doc") '開啟word事先造出之套版
  '移到下一個Table所在位置,因是第一次呼叫,所以會是tables(1)
With wd
  .Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
  .Selection.Move Unit:=wdRow, Count:=3 '以Row為單位向下移3個儲存格

  'Selection.MoveDown Unit:=wdLine, Count:=3 '以Line為單位向下移3行,如果一個Cell是MultiLine時,會先在同一格移動

  .Selection.MoveRight Unit:=wdCell '向右移一個Cell
  .Selection.TypeText Text:="第一格"
  .Selection.MoveRight Unit:=wdCell
  .Selection.TypeText Text:="第二格"
  .Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
  .Selection.MoveRight Unit:=wdCell
  .Selection.TypeText Text:="第三格"
  .Selection.MoveRight Unit:=wdCell
  .Selection.TypeText Text:="第四格"

  .Selection.MoveRight Unit:=wdCell '已到表格最右,故該指令會新增一個Row
  .Selection.TypeText Text:="NewLine" + vbCrLf + "2'line"
  ww.Tables(1).Select  '選取全部表格
  .Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter '設定文字居中

  .Selection.MoveDown Unit:=wdLine '只是為了讓選取消失
  ww.SaveAs FileName:="NewFile"  '另存新檔
  MsgBox "create table OK", vbInformation

  '如果想讓Word97能顯示出來,要執行以下兩行

  '.Visible = True
  'AppActivate wd.Application.Caption

  '令VB的執行停在這裡,否則Word97馬上會被以下指令所結束
  MsgBox "Press any Key to Close Word", vbInformation
  'ww.Close savechanges:=False
  wd.Quit savechanges:=False  '離開Word97且不存檔
End With
Set ww = Nothing
Set wd = Nothing
End Sub