wordapplication+worddocument
来源:互联网 发布:八爪鱼采集器 淘宝 编辑:程序博客网 时间:2024/06/06 14:01
打开DELPHI
转到severs组件板
拉一个WordApplication1,改名为WordApplication
一个WordDocument1改名为WordDocument
转到dialogs组件板
拉一个OpenDialog1,改名为dlgopen
再拉4个button,将以下四段代码依次放入就行了
启动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;
/////////////////////////////////////
转到severs组件板
拉一个WordApplication1,改名为WordApplication
一个WordDocument1改名为WordDocument
转到dialogs组件板
拉一个OpenDialog1,改名为dlgopen
再拉4个button,将以下四段代码依次放入就行了
启动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;
/////////////////////////////////////
- wordapplication+worddocument
- DOC文件WordDocument流
- delphi wordapplication控件
- delphi wordapplication控件
- Servers 控件组 Wordapplication 表格 使用
- 基于Visual Studio2010开发office2010办公自动化应用(5)自定义WordDocument
- 如何提高网站的PR值
- BSD下添加格式化硬盘
- Flex RemoteObject and AMFPHP 1.9 教程
- Excel 找不到类型或命名空间名称“Excel”(是否缺少 using 指令或程序集引用?)
- COM组件对象与.NET类对象的相互转换
- wordapplication+worddocument
- ASP.NET 2.0的缓存技术简介
- java中ThreadLocal类使用测试
- 各大搜索引擎关于作弊网页的定义
- 基于数据挖掘技术的客户保有应用研究
- 突然发现:设计系统时会不知不觉就会OO
- 根据radio值动态显示file
- JS+DIV实现自定义Title的显示方式
- 问题:Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME does not point to the JDK