Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-12-2018, 07:03 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 258
Default Delphi5: All excel application closes while saving data using TExcelApplication

Dear Expert,

I am using the code as below to export Tclientdataset data to Excel sheet from delphi5 application. Everything is ok except all the EXCEL application closes while running the code. How should I work around to keep open the existing opened excel file and to save the required data to new Excel file. Kindly help.

HTML Code:
var
          oExcel: TExcelApplication;
          WS : _Worksheet;
begin
          oExcel := TExcelApplication.Create(Self);
          oExcel.DisplayAlerts[0] := False;
          oExcel.Connect;
          oExcel.Workbooks.Add(NULL, 0);

          WS := oExcel.WorkBooks[1].Worksheets[1] as _Worksheet;


          //To format the first column as TEXT.
          WS.Range['A1', 'A1'].EntireColumn.NumberFormat :=  '@';


          //Now write to the cell.
          WS.Cells.Item[1,1] := '0001';


          oExcel.ActiveWorkbook.SaveCopyAs('D:\Test.xlsx',0);

          oExcel.Disconnect;
          oExcel.Quit;
          oExcel := Nil;
Many thanks in advance.
__________________
Rmt
Reply With Quote
  #2  
Old 01-12-2018, 07:53 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,236
Default

I would try removing the oExcel.Quit.
You're already Disconnect, that should be enough if you want to keep the file open
Reply With Quote
  #3  
Old 01-12-2018, 08:02 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 258
Default

Dear Norrit,

Thanks for response. I tried that but the new file being saved as excel is blank in this case.

Regards
__________________
Rmt
Reply With Quote
  #4  
Old 01-16-2018, 05:18 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 258
Default

Dear Expert,

Pls come up with some suggestions.

Regards
__________________
Rmt
Reply With Quote
  #5  
Old 01-16-2018, 09:40 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,236
Default

Remove both
oExcel.Disconnect;
oExcel.Quit;
???

Because both have something to do with how to react after the Save
And why is it a SaveCopyAs, I would've expected a SaveAs method...

But I'm not a big fan of early binding. I would suggest lage binding and using recording a macro in Excel to see how your code should look like.
VBA is easy translatable to Delphi, many examples to be found
Reply With Quote
  #6  
Old 01-18-2018, 07:20 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 258
Default

Dear Norrit,

After removing (oExcel.Disconnect;oExcel.Quit; ) and using

ActiveWorkbook.SaveAs(FileName,xlNormal,EmptyParam ,EmptyParam, EmptyParam, EmptyParam, xlNochange,xlUserResolution,EmptyParam,EmptyParam, EmptyParam,
0)

I got an error OLE Error 800AC472.

Another clue:

When using TClientDataSet:

The original code i posted earlier is working as desired in Windows 8 and Office 2010. However, in my system with Windows 7 and Office 2010 the data is saved to excel but all the opened excel files get closed.

When using TADODataSet:
In both Windows 7 and 8, Data is saved to excel but all the opened excel files get closed.

If possible, come up with suggestions using the existing TExcelApplication as I am using this code in various files.

Many Thanks.
__________________
Rmt
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:34 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.