Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
APK file name 7 63
Using idhttp to login to instagram 2 64
LAN or WAN ? 11 59
How to define IfThen functions in one common unit? 4 35
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now