Christian
Sample code:
procedure TForm1.BtnClick(Sender: TObject);
var
UA: Variant;
begin
CoInitialize(nil);
UA := CreateOleObject('UA.API2');
// UA := Unassigned;
CoUninitialize; <-- AV here (also with above line uncommented
end;
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
"Christian Holzner" <noner@none> wrote in message
news:3d7f...@newsgroups.borland.com...
But your hint with placing them into OnFormCreate/OnFormDestroy worked - no
AV any more. Thanks!
Christian
"Dmitry Streblechenko" <dmi...@dimastr.com> wrote in message
news:3d7f7f79$2...@newsgroups.borland.com...
constructor TExcelR.Create(AOwner:TComponent);
(********************************************************************)
(* Constructor for the TExcelR object specifying an owner component.
(********************************************************************)
begin
CoInitialize(AOwner);
ExcelApplication1:=TExcelApplication.Create(AOwner);
WorkBook:=TExcelWorkBook.Create(AOwner);
WorkSheet:=TExcelWorksheet.Create(AOwner);
lcid:=GetUserDefaultLCID;
ExcelApplication1.AutoConnect:=true;
ExcelApplication1.Visible[lcid]:=true;
ExcelApplication1.DisplayAlerts[lcid]:=false;
end;
destructor TExcelR.Destroy;
(*********************************************************************)
(* Destructor for the TExcelR object.
(*********************************************************************)
begin
WorkSheet.Free; WorkBook.Free; ExcelApplication1.Free;
CoUninitialize;
end;
This works fine in programs that have forms (i.e. with a call in the DPR
file to Application.Initialize). However, I want to use the wrapper in a
Delphi program that doesn't have forms, and hence doesn't have the call
to Application.Initialize. Typical code in the DPR file is:
var ExcelFile:TExcelR;
begin
ExcelFile:=TExcelR.Create(nil);
<code in here to read/write to the Excel file>
ExcelFile.Destroy;
end;
It worked fine doing this on a machine running Windows ME, but I
upgraded recently to XP, and the problem has appeared. Incidently, it
also gave the same problem on a machine running NT4.
I need to have the CoInitialize in the constructor or else the same
error as Christian mentioned occurs, and if I comment out the
CoUnInitialize in the destructor, the error doesn't occur, but maybe I
am not freeing up memory or something if I do this?
Any help would be gratefully received.
Robin Matthews.
+++++