Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error exporting to MS Excel

Status
Not open for further replies.

espositophp

Programmer
Sep 30, 2003
31
Hello, I have developed a program that exports the content of a StringGrid to MS Excel.

On my computer it works just fine but a lot of users who have installed my app told me they got the following error message:

Class not registered, ProgID: "Excel.Application".


This is the code I have used:

Code:
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComObj, Grids, ExtCtrls, StdCtrls, uSQLite3, SQLiteTable3;
 
 
function SaveAsExcelFile(AGrid: TStringGrid; ASheetName, AFileName: string): Boolean;
const
  xlWBATWorksheet = -4167;
var
  Row, Col: Integer;
  GridPrevFile: string;
  XLApp, Sheet, Data: OLEVariant;
  i, j: Integer;
begin
 
  Screen.Cursor := crHourGlass;
 
  // Prepare Data
  Data := VarArrayCreate([1, AGrid.RowCount, 1, AGrid.ColCount], varVariant);
 
  for i := 0 to AGrid.ColCount - 1 do
    for j := 0 to AGrid.RowCount - 1 do
      Data[j + 1, i + 1] := Trim(AGrid.Cells[i, j]);
 
  // Create Excel-OLE Object
  Result := False;
  XLApp := CreateOleObject('Excel.Application');
 
  try
    // Hide Excel
    XLApp.Visible := False;
    // Add new Workbook
    XLApp.Workbooks.Add(xlWBatWorkSheet);
    Sheet := XLApp.Workbooks[1].WorkSheets[1];
    Sheet.Name := ASheetName;
 
    Sheet.Range[RefToCell(1, 1), RefToCell(AGrid.RowCount, AGrid.ColCount)].Select;
    XLApp.Selection.NumberFormat := '@';
 
    // Fill up the sheet
    Sheet.Range[RefToCell(1, 1), RefToCell(AGrid.RowCount, AGrid.ColCount)].Value := Data;
 
    // Show Excel Worksheet
    try
 
      // Rende Bold le celle dei titoli
      XLApp.Application.Cells[1, 2].Font.Bold := True;
      XLApp.Application.Cells[1, 3].Font.Bold := True;
      XLApp.Application.Cells[1, 4].Font.Bold := True;
      XLApp.Application.Cells[1, 5].Font.Bold := True;
      XLApp.Application.Cells[1, 6].Font.Bold := True;
      XLApp.Application.Cells[1, 7].Font.Bold := True;
      XLApp.Application.Cells[1, 8].Font.Bold := True;
      XLApp.Application.Cells[1, 9].Font.Bold := True;
      XLApp.Application.Cells[1, 10].Font.Bold := True;
      XLApp.Application.Cells[1, 11].Font.Bold := True;
      XLApp.Application.Cells[1, 12].Font.Bold := True;
      XLApp.Application.Cells[1, 13].Font.Bold := True;
      XLApp.Application.Cells[1, 14].Font.Bold := True;
      XLApp.Application.Cells[1, 15].Font.Bold := True;
      XLApp.Application.Cells[1, 16].Font.Bold := True;
      XLApp.Application.Cells[1, 17].Font.Bold := True;
      XLApp.Application.Cells[1, 18].Font.Bold := True;
      XLApp.Application.Cells[1, 19].Font.Bold := True;
      XLApp.Application.Cells[1, 20].Font.Bold := True;
      XLApp.Application.Cells[1, 21].Font.Bold := True;
 
      XLApp.Columns['A:U'].EntireColumn.AutoFit;
 
      XLApp.Cells[1,1].Select;
 
      //Show Excel
      XLApp.Visible := True;
      Screen.Cursor := crDefault;
 
    except
      Screen.Cursor := crDefault;
      ShowMessage('An unforeseen error has occurred while exporting to Excel.');
    end;
 
  finally
    Screen.Cursor := crDefault;
  end;
 
end;

What am I missing?

Any help will be greatly appreciated.
 
Thank you very much indeed, whosrdaddy.

My error was to embed the SQL3 library in the executable instead of deploying it as an external file.

Any further confirmation will be appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top