Delete row in Excel from Delphi program

I want to delete rows from Excel using Delphi7 program.
This throws an exception:

Excel := CreateOleObject('Excel.Application');  
...  
Excel.ActiveWorkBook.Rows(row).Delete;  

What am I doing wrong?

+3
source share
2 answers

Rowsis a property of the worksheet . Therefore, this should work:

Excel.ActiveWorkBook.ActiveSheet.Rows[row].Delete;

See the Excel Object Model Reference Model .

+2
source

The following works for me:

var
  Excel: ExcelApplication;
  Workbook: ExcelWorkbook;
  Sheet: ExcelWorksheet;
begin
  Excel := CoExcelApplication.Create;
  Workbook := Excel.Workbooks.Add(EmptyParam, LOCALE_USER_DEFAULT);
  Sheet := Workbook.ActiveSheet as ExcelWorksheet;
  Sheet.Range['A1','A1'].EntireRow.Delete(EmptyParam);
end;

Please note that I use early binding, which makes life much easier. Just turn on the Excel2000 block and this code will work for you.

, , .

, , Sertac, :

Excel.ActiveSheet.Rows[1].Delete;

, !

+2

All Articles