MasteringVFP/13/5
出自VFP Wiki
實例應用1-在VFP中使用 Word 輸出報表
我們可以在VFP中呼叫COM,例如WORD或ADO。因此可以透過ADO與SQL server連線,做資料的處理,省去了odbc的設定。
以下以WORD為例。先由dbf檔建立word的表格,再以表格與檔案做合併列印。這樣做比用vfp的報表麻煩,但是可以給使用者較大的彈性。
* 建立資料檔
loWord = CreateObject('Word.Application')
with loword
IF thisform.chkProgress.value=1
.visible = -1
ENDIF
.Documents.Add
.ActiveDocument.Tables.Add(.Selection.Range, lnCnt+1, 2)
with .Selection
.TypeText ("系主任姓名")
.MoveRight(12) && Unit:=wdCell
.TypeText ("件數")
go top in rectemp
For I = 1 to lnCnt
.MoveRight (12) && Unit:=wdCell
.TypeText (alltrim(rectemp.inst_name)+' '+ ;
rectemp.name+rectemp.title)
.MoveRight (12) && Unit:=wdCell
.TypeText (alltrim(str(rectemp.dep_cnt)))
skip in rectemp
EndFor
endwith
.ChangeFileOpenDirectory(fullpath('.'))
.ActiveDocument.SaveAs("系主任姓名.doc")
.ActiveDocument.Close
endwith
* 開啟合併檔
loWord.Documents.open('命題函 (系主任).doc')
IF thisform.chkProgress.value=1
loWord.visible = -1
ENDIF
* loDoc = loWord.ActiveDocument
With loWord.ActiveDocument.MailMerge
.Destination = 0 && wdSendToNewDocument
.MailAsAttachment = 0 &&False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = -1 && True
With .DataSource
.FirstRecord = 1 && wdDefaultFirstRecord
.LastRecord = -16 && wdDefaultLastRecord
EndWith
.Execute(-1) && Pause:=True
EndWith
loWord.Windows("命題函 (系主任).doc").Activate
loWord.ActiveWindow.Close
loWord.Move(21,24) && Left:=21, Top:=24
loWord.visible=-1