关于delphi+word的问题,分不够可以加 如何使用delphi控制word,比如我想把数据库中的一些数据生成word文档,并将这个文档保存到一个大字段中,怎么写这个文档,我如果使用模版,那么又是如何控制模版,使模版中相应的地方替换成我想要生成的内容, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://lifeandwork.myrice.com/download/program/Delphi+Word.htmDelphi + Word = 数据库 + 公文处理 delphi擅长做数据库类的mis开发,但对于oa就有点力不从心了。不过随着microsoft的com技术逐渐成熟,现在普通windows应用已经可以和office 97无缝结合了,尤其是在delphi 5中提供了一组servers组件,更是简化了程序开发。 最近接触了一个用户的案例,用delphi控制word做一个合同管理程序。办公人员先根据业务需要,写好合同的文字,但在用户名称、产品名称等变化的位置填写指定的标记字符串,然后通过delphi把数据库中的实际数据替换掉word中的文字,最后让word打印出合同。 delphi自带了一个简单的word例题,但功能太简单。通过查找vba的说明,再对照delphi的vcl,编写了如下代码,实现了基本的公文管理功能。 启动word时用如下代码: begintry 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类似的产品了。 我在xe2中使用idhttp,如何取得sessionID 请帮忙把c++2次开发接口转为delphi,声明! Image 如何在程序中换图片?? 一个dbgrid的问题 各位大虾请进 为什么finalization的MessageBox看不到? 什么是ApplicationEvents控件? 各位师兄:我想作共享软件。但是抛去技术不考虑,我不知道作具体哪一类软件,请告诉我怎样才能得到市场的需求 请问如何实现用回车健互相跳转edit1? delphi field name missing(急) listview问题? 谁有Delphi编写的打字练习软件源码?
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;
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类似的产品了。