为什么delphi6的帮助里找不到关于TWordApplication的内容?在线等待.. 为什么delphi6的帮助里找不到关于TWordApplication的内容?那你能得到这些帮助? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如何使用TWordApplication:最近接触了一个用户的案例,用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类似的产品了。 在DEIPHI中如何调用WORD打开WORD文档unit Uword;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleServer, Word97,ComObj, Buttons;type TForm1 = class(TForm) Button1: TButton; WordApplication: TWordApplication; WordDocument: TWordDocument; BitBtn1: TBitBtn; procedure Button1Click(Sender: TObject); procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);var Template,NewTemplate,ItemIndex:OleVariant; start,stop : olevariant ; myRange:range;Begin Template := EmptyParam; NewTemplate := True; ItemIndex := 1; start := 3 ; stop := 3 ; try Wordapplication.Connect; except MessageDlg('Word may not be installed', mtError, [mbOk], 0); Abort; end; Wordapplication.Visible := True; WordApplication.Caption := 'Delphi automation'; {Create new document} Template := EmptyParam; NewTemplate := False; WordApplication.Documents.Add(Template, NewTemplate); WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex)); WordDocument.Range.InsertAfter('报表'+#13); WordDocument.Range.InsertAfter(#13); //-- 指定插入表的起始处 -------- // myRange := WordDocument.Range(start,stop) ; WordDocument.Tables.Add(myRange,11,2); wordDocument.Tables.Item(1).Columns.Item(1).Width :=20; //改变表格列宽 WordDocument.Range.InsertAfter('正文一:');end;procedure TForm1.BitBtn1Click(Sender: TObject);var Word2000: Variant; begin try Word2000:=CreateOleObject('word.basic'); Word2000.FileNew; Word2000.font('宋体'); //设置字体 Word2000.FontSize(14); //设置字号 Word2000.CenterPara; //居中 Word2000.Insert( '居中'+#13); Word2000.LeftPara; //左对齐 Word2000.Insert( '左对齐'+#13); Word2000.RightPara; //右对齐 Word2000.Insert( '右对齐'+#13); Word2000.AppShow; //显示应用程序 except showmessage('运行 Microsoft Word 失败!'); endend;end.我只的文档是已经存在的。不是自己写一个新的。放两个组件,TWordApplication,TWordDocument.procedure TForm1.Button1Click(Sender: TObject);var Template,NewTemplate,ItemIndex:OleVariant;Begin Template := EmptyParam; NewTemplate := false; ItemIndex:=1; WordApplication1.connect; WordApplication1.visible:=true;//打开WORD WordApplication1.Documents.Add(Template, NewTemplate); WordDocument1.ConnectTo(WordApplication.Documents.Item(ItemIndex)); //打开文档end; 谁给个delphi6.0+SP2 怎样在word表格中动态插入数据? 200分问题,请帮助解决。谢谢 为什么DBGrid不能取序? 请问各位大侠哪里有学习Indy的资料,最好是中文的? 关于 treeview 给分的时候,"贴子回复次数大于跟给分次数 "是什么意思? 这个问题很弱智! DELPHI中ado的使用 请大家评估:我们的“邮件总管”做得如何?能卖多少RMB?需要如何改进?增加什么功能? 请问哪里有比较详细实用的打印教程? 关于.dfm文件中的中文字符问题
最近接触了一个用户的案例,用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类似的产品了。
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleServer, Word97,ComObj, Buttons;type
TForm1 = class(TForm)
Button1: TButton;
WordApplication: TWordApplication;
WordDocument: TWordDocument;
BitBtn1: TBitBtn;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
Template,NewTemplate,ItemIndex:OleVariant;
start,stop : olevariant ;
myRange:range;
Begin
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
start := 3 ;
stop := 3 ;
try
Wordapplication.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation';
{Create new document}
Template := EmptyParam;
NewTemplate := False;
WordApplication.Documents.Add(Template, NewTemplate);
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
WordDocument.Range.InsertAfter('报表'+#13);
WordDocument.Range.InsertAfter(#13);
//-- 指定插入表的起始处 -------- //
myRange := WordDocument.Range(start,stop) ;
WordDocument.Tables.Add(myRange,11,2);
wordDocument.Tables.Item(1).Columns.Item(1).Width :=20; //改变表格列宽
WordDocument.Range.InsertAfter('正文一:');
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var
Word2000: Variant;
begin
try
Word2000:=CreateOleObject('word.basic');
Word2000.FileNew;
Word2000.font('宋体'); //设置字体
Word2000.FontSize(14); //设置字号
Word2000.CenterPara; //居中
Word2000.Insert( '居中'+#13);
Word2000.LeftPara; //左对齐
Word2000.Insert( '左对齐'+#13);
Word2000.RightPara; //右对齐
Word2000.Insert( '右对齐'+#13);
Word2000.AppShow; //显示应用程序
except
showmessage('运行 Microsoft Word 失败!');
end
end;end.我只的文档是已经存在的。不是自己写一个新的。
放两个组件,TWordApplication,TWordDocument.procedure TForm1.Button1Click(Sender: TObject);var Template,NewTemplate,ItemIndex:OleVariant;Begin Template := EmptyParam; NewTemplate := false; ItemIndex:=1; WordApplication1.connect; WordApplication1.visible:=true;//打开WORD WordApplication1.Documents.Add(Template, NewTemplate); WordDocument1.ConnectTo(WordApplication.Documents.Item(ItemIndex)); //打开文档end;