FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Unable to Delete Sheet in Excel File
Posts: 1598
Joined: Fri Oct 07, 2005 05:56 PM
Unable to Delete Sheet in Excel File
Posted: Thu Sep 01, 2011 06:05 PM
I've a problem with delete method in excel. I would like to delete a sheet with name "DATA".
it found this sheet in xls file but after this method oExcel:WorkSheets( n ):Delete(). The DATA sheet is still exist in excel file. I don't know what does it wrong?

Regards,
Dutch
Code (fw): Select all Collapse
#include 'fivewin.ch'

FUNCTION MAIN()
   LOCAL oExcel, oSheet
   LOCAL nRow, nSheets, n
   LOCAL nCounter, nStart, nSeconds, nSecOle, nSecClip, nSecArray
   LOCAL cMemo, cData, aData

   set century on
   set epoch to 1950

   oExcel = CREATEOBJECT( "Excel.Application" )
   oExcel:WorkBooks:Open("d:\fwh\samples\testxls.xls")
   nSheets := oExcel:Sheets:Count()  
   for n := 1 to nSheets
        if upper( oExcel:WorkSheets( n ):Name )=="DATA"
           oExcel:WorkSheets( n ):Delete()
           msginfo('delete')
        end
   next

   oExcel:Visible := .T.
   oExcel:Quit()

return nil
Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Unable to Delete Sheet in Excel File
Posted: Thu Sep 01, 2011 07:34 PM
dutch wrote:
Code (fw): Select all Collapse
nSheets := oExcel:Sheets:Count()


Try

Code (fw): Select all Collapse
nSheets := oExcel:WorkSheets:Count()


EMG
Posts: 1598
Joined: Fri Oct 07, 2005 05:56 PM
Re: Unable to Delete Sheet in Excel File
Posted: Fri Sep 02, 2011 01:04 AM
Dear EMG,

oExcel:Count() is not a problem, it works fine. oExcel:WorkSheets(n):name is work correctly too. But the oExcel:Worksheets:Delete() do nothing. I don't know why.

Thx EMG
Enrico Maria Giordano wrote:
dutch wrote:
Code (fw): Select all Collapse
nSheets := oExcel:Sheets:Count()


Try

Code (fw): Select all Collapse
nSheets := oExcel:WorkSheets:Count()


EMG
Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Unable to Delete Sheet in Excel File
Posted: Fri Sep 02, 2011 06:22 AM
Replace

Code (fw): Select all Collapse
oExcel:WorkSheets( n ):Delete()
msginfo('delete')


with

Code (fw): Select all Collapse
oExcel:WorkSheets( n ):Delete()
msginfo('delete')
exit


It works fine here.

EMG
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Unable to Delete Sheet in Excel File
Posted: Fri Sep 02, 2011 07:15 AM

Dutch,

You need to use :SaveAs() method for to obtain permanent changes on your excel workbook

Regards

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Unable to Delete Sheet in Excel File
Posted: Fri Sep 02, 2011 08:12 AM
I've a problem with delete method in excel. I would like to delete a sheet with name "DATA".
it found this sheet in xls file but after this method oExcel:WorkSheets( n ):Delete(). The DATA sheet is still exist in excel file.


Please insert the statement
Code (fw): Select all Collapse
oExcel:DisplayAlerts := .f.

before deleting the sheet.

I don't know what does it wrong?

If the sheet is empty, delete works without any problem.
But when the sheet contains some data, Excel displays an YesNo alert to the user to confirm deletion.
You could not see this alert because Excel is still not visible. In such a case, excel simply assumes that the user did not accept the deletion. In case you made excel visible earlier to calling Delete() method, you would have seen the dialog.

By assigning .f. to the property DisplayAlerts, we asked Excel to execute the method without waiting for confirmation.
Regards



G. N. Rao.

Hyderabad, India
Posts: 1598
Joined: Fri Oct 07, 2005 05:56 PM
Re: Unable to Delete Sheet in Excel File
Posted: Sun Sep 04, 2011 05:52 PM
Dear All,

Thank you so much for your kindness help. All of your idea are correct.
@EMG, I've changed the do while loop instead of for..next,
@hmpaquito, I've inserted oExcel:Save() at the end before :quit()
@Rao, Your statement is True, the DATA sheet has some data. The DisplayAlerts must required.
Code (fw): Select all Collapse
#include 'fivewin.ch'
#define GTI_CLIPBOARDDATA 15

FUNCTION MAIN()
   LOCAL oExcel, oSheet
   LOCAL nRow, nSheets, n
   LOCAL nCounter, nStart, nSeconds, nSecOle, nSecClip, nSecArray
   LOCAL cMemo, cData, aData

   set century on
   set epoch to 1950

   oExcel = CREATEOBJECT( "Excel.Application" )
   
   oExcel:WorkBooks:Open("d:\fwh\samples\testxls.xls")
   oExcel:DisplayAlerts := .F.
   oExcel:Visible := .F.
   nSheets := oExcel:Sheets:Count()  
   n := 1
   do while n <= nSheets
        if upper( oExcel:WorkSheets( n ):Name )=="DATA"
           oExcel:WorkSheets( n ):Delete()
           exit
        end
        n++
   end
   oExcel:Save()
   oExcel:Quit()



Enrico Maria Giordano wrote:Replace

Code (fw): Select all Collapse
oExcel:WorkSheets( n ):Delete()
msginfo('delete')


with

Code (fw): Select all Collapse
oExcel:WorkSheets( n ):Delete()
msginfo('delete')
exit


It works fine here.

EMG

hmpaquito wrote:Dutch,

You need to use :SaveAs() method for to obtain permanent changes on your excel workbook


Regards


nageswaragunupudi wrote:
I've a problem with delete method in excel. I would like to delete a sheet with name "DATA".
it found this sheet in xls file but after this method oExcel:WorkSheets( n ):Delete(). The DATA sheet is still exist in excel file.


Please insert the statement
Code (fw): Select all Collapse
oExcel:DisplayAlerts := .f.

before deleting the sheet.

I don't know what does it wrong?

If the sheet is empty, delete works without any problem.
But when the sheet contains some data, Excel displays an YesNo alert to the user to confirm deletion.
You could not see this alert because Excel is still not visible. In such a case, excel simply assumes that the user did not accept the deletion. In case you made excel visible earlier to calling Delete() method, you would have seen the dialog.

By assigning .f. to the property DisplayAlerts, we asked Excel to execute the method without waiting for confirmation.
Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)

Continue the discussion