Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Excel Sheet color

Posted on 2009-02-18
3
Medium Priority
?
667 Views
Last Modified: 2012-05-06
i have to copy the excel sheet from one excel work book to another workbook at run time using the delphi code.
source excel sheet is formated with some colors
i have done it sucessfully.
but when i newly created work sheet entire work sheet is painted with Green color

can some body give me idea how to retain the same color format in the new work sheet?
0
Comment
Question by:SecureMeters
  • 2
3 Comments
 
LVL 2

Expert Comment

by:Bongos
ID: 23678983
Can you show the code you're using to perfom the sheet copy?
0
 

Author Comment

by:SecureMeters
ID: 23680046
procedure TForm1.Button8Click(Sender: TObject);
var
wbSource : _WorkBook;
wbDestination: _WorkBook;
fileLocationSource : String;
fileLocationDestination : String;
FileAvail:boolean;
Sheets: Variant;
begin
try
Exlapp:=TExcelApplication.Create(nil);

OpenDialog1.Execute;
fileLocationSource := OpenDialog1.FileName;
fileLocationDestination := 'D:\111.xls';
exlapp.Visible[0]:=true;
//opening both the source and the destination workbooks
 Exlapp.Workbooks.Open(fileLocationSource,EmptyParam,EmptyParam,EmptyParam
                        ,EmptyParam,EmptyParam,EmptyParam,EmptyParam
                        ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
 wbSource := (exlapp.ActiveWorkbook);
 wbDestination := Exlapp.Workbooks.add(Null,0);
 wbDestination := (exlapp.ActiveWorkbook);
 FileAvail:=FileExists(fileLocationDestination);
 if not FileExists(fileLocationDestination) then
 begin
  wbDestination.SaveAs(fileLocationDestination,EmptyParam,EmptyParam,EmptyParam,
                      EmptyParam,EmptyParam,0,
                      EmptyParam,EmptyParam,EmptyParam,
                      EmptyParam,0);

 end;
 Exlapp.Workbooks.Open(fileLocationDestination,EmptyParam,EmptyParam,EmptyParam
                        ,EmptyParam,EmptyParam,EmptyParam,EmptyParam
                        ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
 wbDestination := (exlapp.ActiveWorkbook);
 wbSource.Worksheets.Copy(null,wbDestination.Worksheets[wbDestination.Worksheets.count],0);
  if not FileAvail then
  begin
   Sheets := Exlapp.Sheets;
   while Sheets.count<>1 do
    Sheets.item[1].delete;
  end;
  OleVariant(Exlapp.ActiveWorkbook.Worksheets[1]).Name :='aaa';
  wbDestination.Save(0);
  wbDestination.Close(emptyparam,emptyparam,emptyparam,0);
  wbsource.Close(emptyparam,emptyparam,emptyparam,0);

finally
  exlapp.Free;
 end;
end;
0
 
LVL 2

Accepted Solution

by:
Bongos earned 1500 total points
ID: 23680195
That works fine for me (with a couple of minor amendments). I certainly don't see the colour issues that you're having. I'm using Excel 2003/Delphi6. Sorry.
var
wbSource : _WorkBook;
wbDestination: _WorkBook;
fileLocationSource : String;
fileLocationDestination : String;
FileAvail:boolean;
Sheets: Variant;
Exlapp:         TExcelApplication;
begin
  if not OpenDialog1.Execute then
    Exit;
  Exlapp := TExcelApplication.Create(nil);
  try
    fileLocationSource := OpenDialog1.FileName;
    fileLocationDestination := 'C:\111.xls';
    exlapp.Visible[0]:=true;
    //opening both the source and the destination workbooks
    wbSource := Exlapp.Workbooks.Open(fileLocationSource,EmptyParam,EmptyParam,EmptyParam
                            ,EmptyParam,EmptyParam,EmptyParam,EmptyParam
                            ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
//    wbSource := (exlapp.ActiveWorkbook);
    wbDestination := Exlapp.Workbooks.add(Null,0);
//    wbDestination := (exlapp.ActiveWorkbook);
    FileAvail:=FileExists(fileLocationDestination);
    if not FileExists(fileLocationDestination) then
    begin
      wbDestination.SaveAs(fileLocationDestination,EmptyParam,EmptyParam,EmptyParam,
                          EmptyParam,EmptyParam,0,
                          EmptyParam,EmptyParam,EmptyParam,
                          EmptyParam,0);
 
    end;
    wbDestination := Exlapp.Workbooks.Open(fileLocationDestination,EmptyParam,EmptyParam,EmptyParam
                            ,EmptyParam,EmptyParam,EmptyParam,EmptyParam
                            ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
    //wbDestination := (exlapp.ActiveWorkbook);
    wbSource.Worksheets.Copy(null,wbDestination.Worksheets[wbDestination.Worksheets.count],0);
    {if not FileAvail then
    begin
      Sheets := Exlapp.Sheets;
      while Sheets.count<>1 do
        Sheets.item[1].delete;
    end;
    OleVariant(Exlapp.ActiveWorkbook.Worksheets[1]).Name :='aaa';}
    wbDestination.Save(0);
    wbDestination.Close(emptyparam,emptyparam,emptyparam,0);
    wbsource.Close(emptyparam,emptyparam,emptyparam,0);
  finally
    exlapp.Free;
  end;
end;

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

581 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