I just wanted to tell you that I didn´t solve this problem with late-binding but instead with early-binding.
For everyone with the same problem here is a possible solution:
uses excel97, excel2000 //order is important
..
var
ExcelApp : TExcelApplication;
ExcelWb: _WorkBook;
ExcelSt: _WorkSheet;
iLCID: Integer;
Open Excel, load a file, activate a sheet and connect to an OnWorkbookBeforeClose method
// open excel
iLCID := GetUserDefaultLCID;
// create and connect excelapplication
ExcelApp := TExcelApplication.Create(self);
ExcelApp.Connect;
// load file
ExcelWb := ExcelApp.Workbooks.Open(
ExtractFilePath(ParamStr(0))+'test.xlsx',
emptyParam, emptyParam, emptyParam, emptyParam,
emptyParam, emptyParam, emptyParam, emptyParam,
emptyParam, emptyParam, emptyParam, emptyParam,
iLCID
);
// Visible - off
ExcelApp.Application.Visible[0] := false;
// Starts excel on the first sheet
ExcelSt := ExcelWb.Sheets[1] as _WorkSheet;
ExcelSt.Activate(iLCID);
// Visible - on
ExcelApp.Application.Visible[0] := true;
// Connect to the closemethod
ExcelApp.OnWorkbookBeforeClose := WorkbookBeforeClose;
Replace something
var
sWhat, sReplacement : String;
iSheets : Integer;
begin
// basic data
sWhat := **STRING 1**;
sReplacement := **STRING 2**;
// Warnings off (Overridewarning)
ExcelApp.Application.DisplayAlerts[iLCID] := False;
// Replace for all sheets in a workbook
for iSheets := 1 to ExcelApp.Application.Sheets.Get_Count do
begin
ExcelSt := ExcelWb.Sheets[iSheets] as _WorkSheet;
ExcelSt.Activate(iLCID);
ExcelApp.Application.ActiveWorkbook.Application.Cells.Replace(
{What} sWhat,
{Replacement} sReplacement,
{LookAt} xlWhole,
{SearchOrder} xlByRows,
{MatchCase} False,
{MatchByte} False
);
end;
// warnings on
ExcelApp.Application.DisplayAlerts[iLCID] := True;
Save your work
var
oFileName, oFileFormat : OleVariant;
begin
{
Value = Constant in excel (Description)
------------------------------------------------------------------------------
50 = xlExcel12 (Excel Binary Workbook 2007+ with or without macros, xlsb)
51 = xlOpenXMLWorkbook (without macros Excel 2007+, xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macros Excel 2007+, xlsm)
56 = xlExcel8 (97-2003 Format in Excel 2007-2010, xls)
!!! in 2003 = xlNormal !!!
------------------------------------------------------------------------------
}
oFileName := 'Filename';
oFileFormat := 50;
iLCID := GetUserDefaultLCID;
ExcelApp.Application.ActiveWorkbook.SaveAs(
{Filename} oFileName,
{FileFormat} oFileFormat,
{Password} emptyParam,
{WriteResPassword} emptyParam,
{ReadOnlyRecommended} false,
{CreateBackup} false,
{AccessMode} 0,
{ConflictResolution} false,
{AddToMru} false,
{TextCodepage} false,
{TextVisualLayout} false,
{LCID} iLCID
);
BeforeClose method
procedure WorkbookBeforeClose(Sender: TObject;
var Wb, Cancel: OleVariant);
var
oTrue :OleVariant;
begin
ExcelApp.Application.Visible[0] := false;
//Here comes your stuff when the user wants to quit excel
oTrue := true;
Cancel := oTrue;
ExcelApp.Quit;
end;