在delphi中操作word,有如下需求:
1、读取任意doc文档
2、可以选择显示或者不显示word的操作菜单已经做了一下尝试:
1、使用TOleContainer对象,如下来读取:
try
MyWordOLE.CreateObjectFromFile(sFile,false);
MyWordOLE.Run;
if MyWordOLE.State = osRunning then
MyWordOLE.DoVerb(ovPrimary); except
on E: Exception do
begin
Result := False;
Application.MessageBox(Pchar('打开文档 " ' + sFile + ' "失败: ' + E.Message), '系统提示: ', 48);
Exit;
end;
end;
MyWordOLE.DoVerb(ovPrimary);这里如果设置为ovHide,就不会显示word的菜单,但是那样word内容中不会出现滚动条,这样导致不能很好的显示内容,google了一下说是OleContainer自身的bug
2、尝试使用DSOFramer控件,但是在delphi注册后,只要在窗体上放置控件,运行就出现invalid variant type错误,没有找到解决办法!寻求同志们的帮助,解决问题另外加100分!
1、读取任意doc文档
2、可以选择显示或者不显示word的操作菜单已经做了一下尝试:
1、使用TOleContainer对象,如下来读取:
try
MyWordOLE.CreateObjectFromFile(sFile,false);
MyWordOLE.Run;
if MyWordOLE.State = osRunning then
MyWordOLE.DoVerb(ovPrimary); except
on E: Exception do
begin
Result := False;
Application.MessageBox(Pchar('打开文档 " ' + sFile + ' "失败: ' + E.Message), '系统提示: ', 48);
Exit;
end;
end;
MyWordOLE.DoVerb(ovPrimary);这里如果设置为ovHide,就不会显示word的菜单,但是那样word内容中不会出现滚动条,这样导致不能很好的显示内容,google了一下说是OleContainer自身的bug
2、尝试使用DSOFramer控件,但是在delphi注册后,只要在窗体上放置控件,运行就出现invalid variant type错误,没有找到解决办法!寻求同志们的帮助,解决问题另外加100分!
解决方案 »
- 我知道delphi中会出现内存错误,如何在程序中处理这个异常
- 请高手指点(数据库应用程序)
- 如何将上传的文件保存到特定路径,并下载
- 如何读取dat文件,并把内容存在MEMO里面
- 哪位好心人帮我看看下面这个奇怪的问题?
- 如何取得弹出窗口的返回值?
- Delphi下文件关联问题
- 请问:关于ADO主从表在ltBatchOptimistic状态下的更新问题
- 我是用ADSL上网的,外网机器可以访问我的Web服务器吗?
- sql返回的内容,我看不出有什么错,可是总是报错”无效列名”,请帮我看一下
- 关于TWebbroser!
- 两个不同的库。想连接查询他们各自的一张表。有弄过吗?急啊。在线求救啊呀。分不够可以继续开贴啊!
读取和显示Word文档的VCL控件:
http://www.ksdev.com/vcl/index.html
http://vcl.vclxx.org/DELPHI/D32FREE/DATA2WRD.ZIP 最近接触了一个用户的案例,用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类似的产品了。