[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

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

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
mornao
Asked:
mornao
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
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

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now