如下
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Variants,
ExtCtrls, StdCtrls, Registry, OleServer, Word2000;
type
TMainForm = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
Fexcel,sheet: Variant;
function Openexcel: Boolean;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
uses
ComObj;
procedure TMainForm.FormCreate(Sender: TObject);
begin
end;
function TMainForm.Openexcel: Boolean;
var
Curexcel: string;
I: Integer;
begin
try
fexcel:= CreateOleObject(excel.application.10');
except
Application.MessageBox('无法运行 excel97 或 excel2000,' + #13#10
+ '请检查 word 97 或 word 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
try
begin
Fexcel.DisplayAlerts := False;
Fexcel.Visible := True;
SetCurrentDir(ExtractFilePath(Application.Exename));
Fword.documents.Open(ExtractFilePath(Application.Exename) + filename);
Result := True;
end;
except
Application.MessageBox(',',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
end;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if not VarIsEmpty(fexcel) then Fexcel.Quit;
end;
end.