5,388
社区成员
发帖
与我相关
我的任务
分享
uses ComObj, ActiveX
function My_CreateOleObject(const ClassName: string; out Ole_Handle: IDispatch): Boolean;
var
ClassID: TCLSID;
l_Result: HResult;
begin
Result := False;
l_Result := CLSIDFromProgID(PWideChar(WideString(ClassName)), ClassID);
if (l_Result and $80000000) = 0 then
begin
l_Result := CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, IDispatch, Ole_Handle);
if (l_Result and $80000000) = 0 then
Result := True;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
XLApp: OleVariant;
l_Excel_Handle: IDispatch;
begin
try
XLApp := CreateOleObject('Excel.Application');
XLApp.WorkBooks.Open('c:\test.xls');
XLApp.visible := False;
except
on E: Exception do
begin
if not My_CreateOleObject('Excel.Application', l_Excel_Handle) then
begin
XLApp := Unassigned;
raise exception.Create('启动Excel失败,可能没有安装Excel!');
Exit;
end;
end;
end;