作者:楊忠勳專業:電腦軟體開發及應用語言能力:TOFEL633 GRE2140
Email:[email protected]
在Mis系統的實際開發中,我們有時需要將目前頁面上報表的資料以Word文件的格式下載到本機,這種實作並不困難。但有時我們需要對下載的Word文件的格式做一些設置,例如標題顏色,字體大小,字間距等等,這時我們就要用到Word自帶的宏功能。
例如我們想將此報表的標題在Word文件中以如下格式顯示:14號字,加粗,居中對齊。首先我們需要在Word中錄製對應的巨集指令。開啟Word,新建一文檔,手動敲入一行字,然後選擇工具->巨集->錄製新巨集指令,為新巨集取一個名字如Macro1,執行以上動作(14號字,加粗,居中對齊), Word會自動將這些動作儲存以對應的Vbscript指令。然後選擇工具->宏->巨集指令,選擇剛才我們定義的巨集Macro1,就可以查看其內容了。在此範例中我們儲存的巨集指令如下:
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter '居中對齊
Selection.Font.Bold = wdToggle '加粗顯示
Selection.Font.Size = 14 '14號字
因為巨集指令的腳本語言是Vbscript,我們不需要做任何改動就可以將上面的語句在VB中使用。這樣,我們就可以寫出如下VB程式碼,實現我們所要求的功能。程式碼如下:
WdApp.Selection.Font.Bold = wdToggle '加粗顯示
WdApp.Selection.Font.Size = 14 '14號字
WdApp.Selection.TypeText ("報表標題") '報表標題
WdApp.Selection.ParagraphFormat.lignment = wdAlignParagraphCenter '置中對齊
WdApp.Selection.Font.Bold = wdToggle '取消加粗
同樣,我們如想對Word文檔進行其他處理,重複以上的步驟就可以了。以下提供我的一個完整的對Word文檔進行處理的例子:
Private Function SaveAsWord(ByRef MyRecord As Recordset, ByVal DocFileName As String, ByRef OutMessage As String) As Integer
'************************************************* ************************
'
'說明:將資料集中的資料另存為DOC文件
'
'參數:
'
'MyRecord 資料集
'DocFileName WORD檔案的名稱(無路徑,路徑見實例變數sPath)
'OutMessage 操作的的回傳訊息
'
'返回: 1成功 -1失敗
'
'************************************************* ************************
'初始化Word應用
err.Clear
On Error GoTo Err_All
Dim WdApp As Word.Application
Set WdApp = CreateObject("Word.Application")
'插入數據
Dim colloop As Integer '列號
Dim rowloop As Integer '行號
Dim colMax As Integer '列數
Dim rowMax As Integer '行數
Dim wdcell As Integer '寬
Dim UnitEnd As Integer '截取結束點
Dim UnitName As String '單位名稱
Dim BbDate As String '報表期別
wdcell = 12
colMax = MyRecord.Fields.count
rowMax = MyRecord.RecordCount
WdApp.Documents.Add
'取得報表單位
UnitEnd = InStr(sBBDetail, "期別")
UnitName = Mid(sBBDetail, 1, UnitEnd - 2)
BbDate = Mid(sBBDetail, UnitEnd, Len(sBBDetail))
If MyRecord.Fields.count >= 10 Then
WdApp.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
Else
WdApp.ActiveDocument.PageSetup.Orientation = wdOrientPortrait
End If
'報表名稱
WdApp.Selection.Font.Bold = wdToggle
WdApp.Selection.Font.Size = 14
WdApp.Selection.TypeText (sbbmc)
WdApp.Selection.ParagraphFormat.lignment = wdAlignParagraphCenter
WdApp.Selection.Font.Bold = wdToggle
WdApp.Selection.TypeParagraph
'報表單位名稱
WdApp.Selection.Font.color = wdColorBlack
WdApp.Selection.Font.Size = 11
WdApp.Selection.TypeText (UnitName)
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
WdApp.Selection.TypeParagraph
'報表期別
WdApp.Selection.TypeText (BbDate)
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
WdApp.Selection.TypeParagraph
WdApp.Selection.TypeParagraph
'生成列頭
'wdApp.Selection.HomeKey wdLine, wdExtend
'dApp.Selection.Font.Bold = wdToggle
WdApp.ActiveDocument.Tables.Add WdApp.Selection.Range, rowMax, colMax
Dim i As Integer
Do
For colloop = 0 To colMax - 1
WdApp.Selection.Font.Size = 9
If i = 0 Then
'表格中標題加粗顯示
WdApp.Selection.Font.Bold = wdToggle
'表格標題行背景顏色設定為灰色,灰階為30
With WdApp.Selection.Cells
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray30
End With
End With
End If
'最後一行右對齊,其餘左對齊
If i > 0 Then
If MyRecord.Fields.Item(colloop).Name = "ZBMC" Or MyRecord.Fields.Item(colloop).Name = "指標名稱" Then
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Else
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
End If
End If
If i = 0 And (MyRecord.Fields.Item(colloop).Name = "SXH" Or MyRecord.Fields.Item(colloop).Name = "順序號") Then
WdApp.Selection.TypeText ("序號")
Else
WdApp.Selection.TypeText (CStr(MyRecord.Fields.Item(colloop).value))
End If
If (i <> rowMax - 1 Or (i = rowMax - 1 And colloop < colMax - 1)) Then
WdApp.Selection.MoveRight (wdcell)
End If
Next
i = i + 1
MyRecord.MoveNext
Loop Until MyRecord.EOF
WdApp.ActiveDocument.SaveAs DocFileName, 0, False, "", True, "", False, False, False, False, False
WdApp.Quit
SaveAsWord = 1
Exit Function
Err_All:
Set WdApp = Nothing
SaveAsWord = -1
OutMessage = err.Description
Exit Function
End Function
好了,到此為止,我想你們對在VB中利用Word巨集指令開發ASP元件,有了一些了解。只要多使用,很快就會熟悉的。