下面以word为例
要在uses中加入Variants
function TMainForm.Openword: Boolean;
var
fword:Variant;
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;
end;