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 失败!');
end
end;end.
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.
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;
然后使用下面语句就可以直接打开该文档。
ShellExcute(handle,'open','c\test.doc',nil,nil,sw_shownornal);
ShellExecute(handle,'open','c\test.doc',nil,nil,sw_shownornal);
--------------------------------------------------------------------------------
具体的做法我已经告诉你了。
使用WordApplication1.Documents.Open
单是设计那些参数就够你受的了。
itemindex :olevariant;
filename, confirmconversions, readonly, addtorecentfiles,
passworddocument, passwordtemplate, revert,
writepassworddocument, writepasswordtemplate, format: olevariant;
begin
//打开word
try
wordapplication1.connect;
except
messagedlg('word may not be installed', mterror, [mbok], 0);
abort;
end;
wordapplication1.visible := true;
//打开文件
filename := 'c:\prog\delphi\aa.doc';
confirmconversions := false;
readonly := false;
addtorecentfiles := false;
passworddocument := '';
passwordtemplate := '';
revert := true;
writepassworddocument := '';
writepasswordtemplate := '';
format := wdopenformatdocument;wordapplication1.documents.open( filename, confirmconversions,
readonly, addtorecentfiles, passworddocument, passwordtemplate,
revert, writepassworddocument, writepasswordtemplate, format );
[email protected]