Board index » delphi » Delphi 4.0 and Excel

Delphi 4.0 and Excel

I am attempting to open Excel and populate a few cells.  I used the following
code which worked fine, however when I tried to put it into my applciation I
keep getting the following error CoInitialize has not be called.
Does anyone know of a component that can help or know what is going wrong?

Thanks for your time and help!
Scott

unit Main;

interface

uses
  Windows, Messages, SysUtils,
  Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    XLApp: Variant;
    procedure InsertData;
    procedure ChangeColumns;
    procedure HandleRange;
  public
  end;

var
  Form1: TForm1;

implementation

uses
  ComObj, XLConst;

{$R *.DFM}

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if not VarIsEmpty(XLApp) then begin
    XLApp.DisplayAlerts := False;  // Discard unsaved files....
    XLApp.Quit;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  XLApp:= CreateOleObject('Excel.Application');
  XLApp.Visible := True;
//  XLApp.Workbooks.Add(xlWBatWorkSheet);
  XLApp.Workbooks.Add('c:\Expense.xls');
//  XLApp.Workbooks[1].WorkSheets[1].Name := 'Delphi Data';
  InsertData;
//  HandleRange;
//  ChangeColumns;
end;

procedure TForm1.InsertData;
var
  i: Integer;
  Sheet: Variant;
begin
//  Sheet := XLApp.Workbooks[1].WorkSheets['Delphi Data'];
  Sheet := XLApp.Workbooks[1].WorkSheets['Sheet1'];
//  for i := 1 to 10 do
//    Sheet.Cells[i, 1] := i;
  for i := 10 to 16 do
    Sheet.Cells[i, 9] := i + 100;

//  Sheet.Cells[i, 1] := '=Sum(A1:A10)';
end;

procedure TForm1.HandleRange;
var
  Range: Variant;
begin
  Range := XLApp.Workbooks[1].WorkSheets['Delphi Data'].Range['C1:F25'];

  Range.Formula := '=RAND()';
  Range.Columns.Interior.ColorIndex := 3;
  Range.Borders.LineStyle := xlContinuous;
end;

procedure TForm1.ChangeColumns;
var
  ColumnRange: Variant;
begin
  ColumnRange := XLApp.Workbooks[1].WorkSheets['Delphi Data'].Columns;
  ColumnRange.Columns[1].ColumnWidth := 5;
  ColumnRange.Columns.Item[1].Font.Bold := True;
  ColumnRange.Columns[1].Font.Color := clBlue;
end;

end.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

 

Re:Delphi 4.0 and Excel


Quote
: spin...@mms-inc.com wrote:
>I am attempting to open Excel and populate a few cells.  I used the following
>code which worked fine, however when I tried to put it into my applciation I
>keep getting the following error CoInitialize has not be called.
>Does anyone know of a component that can help or know what is going wrong?

There is a change in the COM implementation of Delphi 4 that requires
a slightly different setup. I think that is documented somewhere in
the readme or whatsnew section, but I always mix up these things (4
versions of Delphi and 3 of C++ Builder are too much, sorry).

Either call CoInitialize yourself, or, better, just create a new
application and add your form to that one - IIRC, the "new" .DPR has
code that correctly initializes the COM subsystem.

--
Stefan Hoffmeister    (http://www.econos.de/)
No private email, please, unless expressly invited.

Other Threads