Solved

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

Posted on 2004-04-05
1
730 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 156
Help on project with Soap 10 57
Downloading email attachments 2 80
RESTRequest Parameter 4 43
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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