delphi擅長作數據庫類的mis開發,但對於oa就有點力不從心了。不過隨着microsoft的com技術逐漸成熟,如今普通windows應用已經能夠和office 97無縫結合了,尤爲是在delphi 5中提供了一組servers組件,更是簡化了程序開發。 最近接觸了一個用戶的案例,用delphi控制word作一個合同管理程序。辦公人員先根據業務須要,寫好合同的文字,但在用戶名稱、產品名稱等變化的位置填寫指定的標記字符串,而後經過delphi把數據庫中的實際數據替換掉word中的文字,最後讓word打印出合同。 delphi自帶了一個簡單的word例題,但功能太簡單。經過查找vba的說明,再對照delphi的vcl,編寫了以下代碼,實現了基本的公文管理功能。 啓動word時用以下代碼: begin try wordapplication.connect; except messagedlg('word may not be installed', mterror, [mbok], 0); abort; end; wordapplication.visible := true; wordapplication.caption := 'delphi automation'; end; 關閉word用以下代碼。若是想保存doc文件,請修改savechanges變量的內容: var savechanges, originalformat, routedocument: olevariant; begin savechanges := wddonotsavechanges; originalformat := unassigned; routedocument := unassigned; try wordapplication.quit(savechanges, originalformat, routedocument); wordapplication.disconnect; except on e: exception do begin showmessage(e.message); wordapplication.disconnect; end; end; end; 讓word打開一個指定的文件,須要先放置opendialog,而後調用wordapplication.documents.open: var itemindex :olevariant; filename, confirmconversions, readonly, addtorecentfiles, passworddocument, passwordtemplate, revert, writepassworddocument, writepasswordtemplate, format: olevariant; begin if not dlgopen.execute then exit; {open document} filename := dlgopen.filename; confirmconversions := false; readonly := false; addtorecentfiles := false; passworddocument := ''; passwordtemplate := ''; revert := true; writepassworddocument := ''; writepasswordtemplate := ''; format := wdopenformatdocument; wordapplication.documents.open( filename, confirmconversions, readonly, addtorecentfiles, passworddocument, passwordtemplate, revert, writepassworddocument, writepasswordtemplate, format ); {assign worddocument component} itemindex := 1; worddocument.connectto(wordapplication.documents.item(itemindex)); {turn spell checking of because it takes a long time if enabled and slows down winword} wordapplication.options.checkspellingasyoutype := false; wordapplication.options.checkgrammarasyoutype := false; end; 讓word替換標記字符串要使用worddocument.range.find.execute,這裏用delphi替換了< #name> : var findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike, matchallwordforms, forward, wrap, format, replacewith, replace: olevariant; begin findtext := '< #name> '; matchcase := false; matchwholeword := true; matchwildcards := false; matchsoundslike := false; matchallwordforms := false; forward := true; wrap := wdfindcontinue; format := false; replacewith := 'delphi'; replace := true; worddocument.range.find.execute( findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike, matchallwordforms, forward, wrap, format, replacewith, replace ); end; 上面這4段代碼完成了公文管理的基本功能,再把它和數據庫結合起來,就能夠開發一個與lotus notes相似的產品了。