Adding 2 more sheets to an excel file

Add-in Express™ Support Service
That's what is more important than anything else

Adding 2 more sheets to an excel file
How can it be done or, why doesn't this code work... 
Fernando Madruga




Posts: 60
Joined: 2006-01-04
Hi.

I have just these lines of code in a controlclick event:


1 procedure TAddInModule.CreateNewWorkbook;
2 var
3   Wb: Excel2000._Workbook;
4 begin
5   ExcelApp.Workbooks.Close( 0 );                    // Close open workbook
6   Wb := ExcelApp.Workbooks.Add( VarNull, 0 );       // Create a new one
7   Wb.Sheets.Add( VarNull,VarNull,2,xlWorksheet,0 ); // Add 2 new sheets
8 end;

When I run it, I get an exception: "error: the add-in has fired an exception OLE error 800A03EC" on the last line. The 1st 2 lines work ok though...

I've tried replacing line 7 with this one:

  ExcelApp.ActiveWorkbook.Sheets.Add( VarNull,VarNull,2,xlWorksheet,0 );

but it throws out the same error again... I've tried playing around with the 1st 2 parameters, but nothing seems to work. What am I doing wrong here??
Posted 16 Jan, 2006 19:37:10 Top
Dmitry Kostochko


Add-in Express team


Posts: 2880
Joined: 2004-04-05
Hi Fernando,

Try the following code.


  ExcelApp.Workbooks.Close(0);
  Wb := ExcelApp.Workbooks.Add(EmptyParam, 0);
  Wb.Sheets.Add(EmptyParam, EmptyParam, 2, xlWorksheet, 0);


Posted 17 Jan, 2006 06:11:07 Top
Fernando Madruga




Posts: 60
Joined: 2006-01-04

That did the work: I just had to add OleCtrls to my uses clause to use EmptyParam, but after that, it works ok!

Thanks,
Fernando Madruga
Posted 19 Jan, 2006 07:56:33 Top