How to delete the sheet in Excel?

How to delete the sheet in Excel?

Anonymous
Not applicable
1,235 Views
13 Replies
Message 1 of 14

How to delete the sheet in Excel?

Anonymous
Not applicable

Hello
I would delete the sheet in Excel.

Can anyone help?

 

(defun c:pol ( / Excel XlsFile WorkSheets SelSht ShtName)
  (setq Excel (vlax-get-or-create-object "Excel.Application")
	XlsFile (vl-catch-all-apply 'vla-open (list (vlax-get-property Excel "WorkBooks") "c:\temp\test.xls")))
  (vlax-invoke-method XlsFile "Activate"); to activate the opened document
  (setq WorkSheets (vl-catch-all-apply 'vlax-get-property (list XlsFile "Worksheets")))
  (setq SelSht (vl-catch-all-apply 'vlax-get-property  (list WorkSheets "Item" 2)))
  (setq ShtName (vla-get-Name SelSht))
  ; =>> Here I want to delete this sheet -> ShtName
  (vl-catch-all-apply 'vlax-invoke-method  (list XlsFile "Save"))
  (vl-catch-all-apply 'vlax-invoke-method  (list XlsFile "Close"))
  (vl-catch-all-apply 'vlax-invoke-method  (list Excel "Quit"))
  (mapcar
    '(lambda (x)
       (vl-catch-all-apply '(lambda ()
			      (progn (vlax-release-object x) (setq x nil)))))
    (list Excel XlsFile WorkSheets SelSht)
    )
  )

 

Jerzy

0 Likes
1,236 Views
13 Replies
Replies (13)
Message 2 of 14

hmsilva
Mentor
Mentor

Hi Jerzy, try

 

(vla-delete SelSht)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 3 of 14

Anonymous
Not applicable

Unfortunately, this is not it.
The program went through all the code, but the effect in the XLS file could not be seen.

 

Jerzy

0 Likes
Message 4 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

Unfortunately, this is not it.
The program went through all the code, but the effect in the XLS file could not be seen.

 

Jerzy


Jerzy,

it works on this side...

 

In some OS the c:\Temp is not accessible to all users, try the code in a directory not in c:\, try for instance in desktop.

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 5 of 14

Anonymous
Not applicable

I replace "c:\temp\test.xls" to 'C:\\Users\\bajj\\Desktop\\test.xls' and still does not work.

I have full permissions on the file.

 

Jerzy

0 Likes
Message 6 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

I replace "c:\temp\test.xls" to 'C:\\Users\\bajj\\Desktop\\test.xls' and still does not work.

I have full permissions on the file.

 

Jerzy


Jerzy,

if you enter

(findfile "C:\\Users\\bajj\\Desktop\\test.xls")

what is the return?

 

I don't have any problem with the (vla-delete SelSht)

 

Before run the code

xls_test-01.PNG

 

after run the code

xls_test-02.PNG

 

Henrique

EESignature

0 Likes
Message 7 of 14

Anonymous
Not applicable

I swapped

 

XlsFile (vl-catch-all-apply 'vla-open (list (vlax-get-property Excel "WorkBooks") (findfile "C:\\temp\\Mosty\\proba.xls"))))

I have included video (video.zip) to show you that for me it's not working.

 

Jerzy

0 Likes
Message 8 of 14

hmsilva
Mentor
Mentor

Jerzy,

I still think that the problems are permissions...

 

 

(defun c:pol (/ Excel XlsFile WorkSheets SelSht ShtName)
    (setq Excel   (vlax-get-or-create-object "Excel.Application")
          XlsFile (vl-catch-all-apply
                      'vla-open
                      (list (vlax-get-property Excel "WorkBooks") (findfile "C:\\temp\\Mosty\\proba.xls"))
                  )
    )
    (vlax-invoke-method XlsFile "Activate")
    (setq WorkSheets (vl-catch-all-apply 'vlax-get-property (list XlsFile "Worksheets")))
    (setq SelSht (vl-catch-all-apply 'vlax-get-property (list WorkSheets "Item" 2)))
    (setq ShtName (vla-get-Name SelSht))
    (if (= (vla-delete SelSht) :vlax-true)
        (alert (strcat "\n " ShtName " was deleted... "))
        (alert (strcat "\n " ShtName " is write-protected.. "))
    )
    (vl-catch-all-apply 'vlax-invoke-method (list XlsFile "Save"))
    (vl-catch-all-apply 'vlax-invoke-method (list XlsFile "Close"))
    (vl-catch-all-apply 'vlax-invoke-method (list Excel "Quit"))
    (mapcar
        '(lambda (x)
             (vl-catch-all-apply
                 '(lambda ()
                      (progn (vlax-release-object x) (setq x nil))
                  )
             )
         )
        (list Excel XlsFile WorkSheets SelSht)
    )
)
Henrique

 

EESignature

0 Likes
Message 9 of 14

Anonymous
Not applicable

I tried a completely 'fresh' file, which I wrote as XLS. And it works.

Thank you.


And the old file was not working, perhaps because the earlier it had an extension XLSX, and I wrote to try it as XLS.

Maybe this stuck my problem.

0 Likes
Message 10 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

I tried a completely 'fresh' file, which I wrote as XLS. And it works.

Thank you.


And the old file was not working, perhaps because the earlier it had an extension XLSX, and I wrote to try it as XLS.

Maybe this stuck my problem.


You're welcome, JerzyBajj
Glad you got it sorted.

 

Henrique

EESignature

Message 11 of 14

Anonymous
Not applicable

Hello
Subject unfortunately returns.
Everything works, provided that cleared layout is empty.
If you are in this layout are any entries that you can not delete it.
How to get around this?
In Excel, it is so that as you delete a blank layout this program do not ask and deletes. How can something tab is before deleting, Excel first asks 'Are you sure'?

0 Likes
Message 12 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

Hello
Subject unfortunately returns.
Everything works, provided that cleared layout is empty.
If you are in this layout are any entries that you can not delete it.
How to get around this?
In Excel, it is so that as you delete a blank layout this program do not ask and deletes. How can something tab is before deleting, Excel first asks 'Are you sure'?


Hi JerzyBajj,

I can't test it at the moment, but delete sheet cells, save, delete the sheet, should do the trick...

(defun c:pol (/ Excel XlsFile WorkSheets SelSht ShtName)
   (setq Excel   (vlax-get-or-create-object "Excel.Application")
         XlsFile (vl-catch-all-apply
                    'vla-open
                    (list (vlax-get-property Excel "WorkBooks") (findfile "D:\\temp\\Mosty\\proba.xls"))
                 )
   )
   (vlax-invoke-method XlsFile "Activate")
   (setq WorkSheets (vl-catch-all-apply 'vlax-get-property (list XlsFile "Worksheets")))
   (setq SelSht (vl-catch-all-apply 'vlax-get-property (list WorkSheets "Item" 2)))
   (setq ShtName (vla-get-Name SelSht))
   (vlax-invoke-method (vlax-get-property SelSht 'cells) 'delete) ; <<<<<
   (vl-catch-all-apply 'vlax-invoke-method (list XlsFile "Save")) ; <<<<<
   (setq SelSht (vl-catch-all-apply 'vlax-get-property (list WorkSheets "Item" 2)))
   (if (= (vla-delete SelSht) :vlax-true)
      (alert (strcat "\n " ShtName " was deleted... "))
      (alert (strcat "\n " ShtName " is write-protected.. "))
   )
   (vl-catch-all-apply 'vlax-invoke-method (list XlsFile "Save"))
   (vl-catch-all-apply 'vlax-invoke-method (list XlsFile "Close"))
   (vl-catch-all-apply 'vlax-invoke-method (list Excel "Quit"))
   (mapcar
      '(lambda (x)
          (vl-catch-all-apply
             '(lambda ()
                 (progn (vlax-release-object x) (setq x nil))
              )
          )
       )
      (list Excel XlsFile WorkSheets SelSht)
   )
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 13 of 14

Anonymous
Not applicable

It works.
Thank you again.

0 Likes
Message 14 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

It works.
Thank you again.


You're welcome, JerzyBajj
Glad I could help

Henrique

EESignature

0 Likes