Unable to Delete Sheet in Excel File

Unable to Delete Sheet in Excel File

Postby dutch » Thu Sep 01, 2011 6: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: Select all  Expand view
#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 19.01 / 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)
User avatar
dutch
 
Posts: 1535
Joined: Fri Oct 07, 2005 5:56 pm
Location: Thailand

Re: Unable to Delete Sheet in Excel File

Postby Enrico Maria Giordano » Thu Sep 01, 2011 7:34 pm

dutch wrote:
Code: Select all  Expand view
nSheets := oExcel:Sheets:Count()


Try

Code: Select all  Expand view
nSheets := oExcel:WorkSheets:Count()


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8367
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Unable to Delete Sheet in Excel File

Postby dutch » Fri Sep 02, 2011 1:04 am

Dear EMG,

oExcel:Sheets: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: Select all  Expand view
nSheets := oExcel:Sheets:Count()


Try

Code: Select all  Expand view
nSheets := oExcel:WorkSheets:Count()


EMG
Regards,
Dutch

FWH 19.01 / 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)
User avatar
dutch
 
Posts: 1535
Joined: Fri Oct 07, 2005 5:56 pm
Location: Thailand

Re: Unable to Delete Sheet in Excel File

Postby Enrico Maria Giordano » Fri Sep 02, 2011 6:22 am

Replace

Code: Select all  Expand view
oExcel:WorkSheets( n ):Delete()
msginfo('delete')


with

Code: Select all  Expand view
oExcel:WorkSheets( n ):Delete()
msginfo('delete')
exit


It works fine here.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8367
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Unable to Delete Sheet in Excel File

Postby hmpaquito » Fri Sep 02, 2011 7:15 am

Dutch,

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


Regards
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: Unable to Delete Sheet in Excel File

Postby nageswaragunupudi » Fri Sep 02, 2011 8: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: Select all  Expand view
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
User avatar
nageswaragunupudi
 
Posts: 10306
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Unable to Delete Sheet in Excel File

Postby dutch » Sun Sep 04, 2011 5: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: Select all  Expand view
#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: Select all  Expand view
oExcel:WorkSheets( n ):Delete()
msginfo('delete')


with

Code: Select all  Expand view
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: Select all  Expand view
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 19.01 / 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)
User avatar
dutch
 
Posts: 1535
Joined: Fri Oct 07, 2005 5:56 pm
Location: Thailand


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 51 guests