Hehe, here is a simple exampe, I wrote it using VC, now I translate
it into Delphi, and I also learned some tech from Delphi4 sample.
hope it might help you.
uses
Word97;
var
WordObject : TWordObject;
WordBasicObject :
GetWordBasic()
procedure TForm1.BtnStartWordClick(Sender: TObject);
begin
if not Assigned(Word8Object) then begin
WordObject := TWordObject.Create;
with WordObject do begin
Visible := True;
OnQuit := AppQuit;
OnDocumentChange := AppDocumentChange;
OnNewDocument := DocNew;
OnOpenDocument := DocOpen;
OnCloseDocument := DocClose;
OpenDocument('Your template file name here'); {把您的模板文件打开}
end;
BtnStartWord.Enabled := False;
BtnClose.Enabled := True;
BtnNewDoc.Enabled := True;
end;
end;
procedure TForm1.BtnCloseClick(Sender: TObject);
begin
if Assigned(WordObject) then begin
BtnStartWord.Enabled := True;
BtnClose.Enabled := False;
BtnNewDoc.Enabled := False;
BtnTypeText.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
procedure TForm1.AppDocumentChange(Sender: TObject);
begin
Memo1.Lines.Add('Document changed');
BtnTypeText.Enabled := WordObject.Application.Documents.Count > 0;
end;
procedure TForm1.AppQuit(Sender: TObject);
begin
Memo1.Lines.Add('Quitting Word');
WordObject.Free;
WordObject := nil;
BtnStartWord.Enabled := True;
BtnClose.Enabled := False;
BtnNewDoc.Enabled := False;
BtnTypeText.Enabled := False;
end;
procedure TForm1.DocNew(Sender: TObject);
begin
Memo1.Lines.Add('New document');
end;
procedure TForm1.DocOpen(Sender: TObject);
begin
Memo1.Lines.Add('Document opened');
end;
procedure TForm1.DocClose(Sender: TObject);
begin
Memo1.Lines.Add('Document closed');
end;
procedure TForm1.BtnNewDocClick(Sender: TObject);
begin
try
WordObject.NewDoc('');
except
ShowMessage('It seems like somebody killed Word and didn''t tell me about it...');
if Assigned(WordObject) then begin
BtnStartWord.Enabled := True;
BtnClose.Enabled := False;
BtnNewDoc.Enabled := False;
BtnTypeText.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
end;
procedure TForm1.BtnTypeTextClick(Sender: TObject);
begin
WordObject.InsertText('This is pegasus speaking,
'); { 插入您想输入的说明}
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(WordObject) then begin
WordObject.Free;
WordObject := nil;
end;
end;