unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,Variants, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Registry, Buttons;
type
TMainForm = class(TForm)
lbl_filename: TLabeledEdit;
lbl_password: TLabeledEdit;
btnok: TButton;
btncancle: TButton;
chkbx_password: TCheckBox;
lbl_svrname: TLabeledEdit;
lbl_user: TLabeledEdit;
procedure FormDestroy(Sender: TObject);
procedure btnokClick(Sender: TObject);
procedure chkbx_passwordClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btncancleClick(Sender: TObject);
private
{ Private declarations }
FTick: UINT;
Fword: Variant;
function Openword: Boolean;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
uses
ComObj;
function GetRegString(RootKey: HKEY; Section, Key, DefaultValue: string): string;
var
MyReg: TRegistry;
begin
Result := DefaultValue;
MyReg := TRegistry.Create;
if MyReg <> nil then
begin
MyReg.RootKey := RootKey;
if MyReg.OpenKeyReadOnly(Section) then
begin
Result := MyReg.ReadString(Key);
if Result = '' then Result := DefaultValue;
MyReg.CloseKey;
end;
MyReg.Free;
end;
end;
function TMainForm.Openword: Boolean;
var
Curword: string;
begin
Curword := GetRegString(HKEY_CLASSES_ROOT, '/word.Application/CurVer', '', '');
if Curword = '' then
begin
Application.MessageBox('请先安装 word 97 或 word 2000!',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
if (Curword <> 'word.Application.8') and (Curword <> 'word.Application.9') then
begin
Curword := GetRegString(HKEY_CLASSES_ROOT, '/word.Application.8', '', '');
if Curword = '' then
begin
Curword := GetRegString(HKEY_CLASSES_ROOT, '/word.Application.9', '', '');
if Curword <> '' then Curword := 'word.Application.9';
end
else
begin
Curword := 'word.Application.8';
end;
if Curword = '' then
begin
Application.MessageBox('无法运行 word 97 或 word 2000,' + #13#10
+ '请检查 word 97 或 word 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
end;
try
Fword := CreateOleObject(Curword);
if VarIsEmpty(Fword) then
begin
Application.MessageBox('无法运行 word 97 或 word 2000,' + #13#10
+ '请检查 word 97 或 word 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
except
Application.MessageBox('无法运行 word 97 或 word 2000,' + #13#10
+ '请检查 word 97 或 word 2000 是否已经正确安装。',
PChar(self.Caption), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
try
begin
Fword.DisplayAlerts := False;
copyfile(pchar(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.mdl'),pchar(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.doc'),true);
SetCurrentDir(ExtractFilePath(Application.Exename));
Fword.documents.Open(ExtractFilePath(Application.Exename) + 'AnalyzeSQL.doc');
Fword.Visible := True;
Fword.Run('test',lbl_svrname.text,lbl_user.Text,lbl_filename.Text,lbl_password.Text);
//宏名称,后面都是宏的参数,有多少就跟多少
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(Fword) then Fword.Quit;
end;
procedure TMainForm.btnokClick(Sender: TObject);
begin
if length(lbl_svrname.text)=0 then lbl_svrname.Text:='(local)';
if length(lbl_filename.Text)=0 then
begin
application.MessageBox(pchar('请输入文件名'),pchar(self.caption),MB_ICONWARNING);
exit;
end;
hide;
if not Openword then Close;
end;
procedure TMainForm.chkbx_passwordClick(Sender: TObject);
begin
lbl_password.Enabled:=chkbx_password.Checked;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
lbl_password.Enabled:=false;
chkbx_password.Checked:=false;
end;
procedure TMainForm.btncancleClick(Sender: TObject);
begin
close;
end;
end.