Solved

Delphi6 OleApplication Excel How to add new worksheets to active workbook.

Posted on 2004-04-05
1
733 Views
Last Modified: 2010-04-16
Have a need to have seven initial worksheets in a workbook, but how do you add another 4 to the three as default? Both the 'add on opening' workbook and the 'add on fly' to an open workbook methods are required please. Code so far...

procedure ExcelOpen(Sender: TObject; Excelname:String);
begin
  // If the workbook exists, open it, else create an instance of it.
  OleApplication := CreateOleObject('Excel.Application');
  OleApplication.visible :=false;
  if fileexists(dataq.outdir+'\'+Excelname) then
  begin
    OleWorkBook:=OleApplication.WorkBooks.open(dataq.outdir+'\'+Excelname);
  end else begin
    OleWorkBook:=OleApplication.WorkBooks.Add;
//    what is the code that should generate the total 7 worksheets?
  end;
end;
 
0
Comment
Question by:mornao
1 Comment
 
LVL 26

Accepted Solution

by:
Russell Libby earned 100 total points
ID: 10757567
The following code will ensure that each workbook you open has 7 worksheets available.

Hope this helps,
Russell



procedure ExcelOpen(Sender: TObject; Excelname:String);
var  OleApplication:   OleVariant;
     OleWorkBook:      Olevariant;
     dwIndex:          Integer;
begin

  // If the workbook exists, open it, else create an instance of it.
  OleApplication:=CreateOleObject('Excel.Application');
  OleApplication.Visible:=False;
  if fileexists(dataq.outdir+'\'+Excelname) then
    OleWorkBook:=OleApplication.WorkBooks.open(dataq.outdir+'\'+Excelname)
  else
     OleWorkBook:=OleApplication.WorkBooks.Add;
  // what is the code that should generate the total 7 worksheets?
  for dwIndex:=OleWorkBook.WorkSheets.Count to 6 do
  begin
     OleWorkBook.Worksheets.Add;
  end;

end;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question