Author Topic: How to truly close Excel  (Read 2399 times)

0 Members and 1 Guest are viewing this topic.

CincyJeff

  • Newt
  • Posts: 89
How to truly close Excel
« on: March 16, 2020, 07:24:19 AM »
If I open, read/write, quit Excel, it stays open under Processes in Task Manager. Is there any way to actually close the application. The following code leaves Excel open. I don't like the fact that the process remains open.
Code: [Select]
(defun XCELTEST (/ *excelapp* file actwb )
  (setq file "C:\\Temp\\TEST.xlsm")
  (setq *excelapp* (vlax-get-or-create-object "Excel.Application"))
  (vlax-for WorkBook (vlax-get-property *excelapp* "WorkBooks")
    (if (= (vlax-get-property WorkBook "Name") file)
      (setq actwb (vlax-invoke-method WorkBook "Activate"))
    ) ;_ end of if
  ) ;_ end of vlax-for
  (if (not actwb)
    (setq actwb
           (vlax-invoke-method
             (vlax-get-property *excelapp* 'WorkBooks)
             'open
             file
           ) ;_ end of vlax-invoke-method
    ) ;_ end of setq
  ) ;_ end of if
  (vlax-invoke-method actwb 'close)
  (vlax-invoke-method *excelapp* 'quit)
  (vlax-release-object *excelapp*)
) ;_ end of defun

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: How to truly close Excel
« Reply #1 on: March 16, 2020, 09:04:41 AM »
(gc)

CincyJeff

  • Newt
  • Posts: 89
Re: How to truly close Excel
« Reply #2 on: March 16, 2020, 10:03:08 AM »
VovKa,
That works for the simple program included in the original post, but if I run a larger routine that closes Excel the same way, including (gc) after the release object call, which was already being called, Excel still stays open in Task Manager. Any ideas?

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: How to truly close Excel
« Reply #3 on: March 16, 2020, 10:12:03 AM »
(gc)(gc) ?

kpblc

  • Bull Frog
  • Posts: 396
Re: How to truly close Excel
« Reply #4 on: March 16, 2020, 10:24:10 AM »
I think there is not the best way to use vlax-get-or-create-object: in this case you can try to use existing Excel application. Change it to vlax-create-object.
And about (gc): try to use somthing like
Code - Auto/Visual Lisp: [Select]
  1. (defun f1()
  2. (f2)
  3. (gc)
  4. )
  5.  
  6. (defun f2()
  7. ;; Excel operations
  8. )
Sorry for my English.

CincyJeff

  • Newt
  • Posts: 89
Re: How to truly close Excel
« Reply #5 on: March 16, 2020, 11:54:03 AM »
kpblc,
I tried vlax-create-object instead of get-or-create with the same result, even with the extra (gc) after the function call. For me, Excel remains open.
I also tried the double (gc) and not running the macro in case it has an effect, no difference.
I'll do some further testing to see if saving the file makes a difference.


Windows 10
Office 365
AutoCAD 2020 1.2

kpblc

  • Bull Frog
  • Posts: 396
Re: How to truly close Excel
« Reply #6 on: March 16, 2020, 03:53:44 PM »
I think Windows won't close any application at time you request it. This task will take some time. Perhaps you have to take some time to completely close external application.
Sorry for my English.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to truly close Excel
« Reply #7 on: March 16, 2020, 04:37:32 PM »
You can kill stubborn windows processes instantly but you have to use this ability with good judgement. Additionally, the sample code I penned in that ^ thread is wild-carded - it will kill all processes matching the passed process name spec - whereas it’s more likely you aim to kill a specific process (pid) - i.e. an excel instance you spawned - rather than all excel instances. Also noting excel supports mdi so it’s possible more than 1 excel workbook is open and may need to be saved or abandoned (if dirty). It’s not difficult to determine all this but you’ll have to do a bit of research to determine same as I’m without the benefit of free time to do it for you. Anyways ... cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

57gmc

  • Bull Frog
  • Posts: 366
Re: How to truly close Excel
« Reply #8 on: March 17, 2020, 10:48:25 AM »
Excel has always had the problem that it doesn't properly release memory for objects it uses. You have to explicitly release the memory by setting your variable to nil. I don't think (gc) is working because it only releases "unused" memory. In the case of Excel, your variables are still "in use" even though they have gone out of scope.  I haven't done this in lisp, but in COM/VBA, you have to release memory of your variables in reverse order in which you created them. For example, you can't set a workbook object to nil before setting a sheet object to nil because the workbook object holds a pointer to the sheet object. Typically, you would set to nil in the following order 1. sheet object, 2. workbook object, 3. application object.

CincyJeff

  • Newt
  • Posts: 89
Re: How to truly close Excel
« Reply #9 on: March 17, 2020, 11:41:22 AM »
kpblc,
I've let it go for minutes and it still doesn't close.

57gmc,
I was already setting my pointers to nil, just not in the sample program I posted, and it still doesn't close.

All,
Instead of putting the close routine under a separate defun, I placed it in the main program and it now closes Excel as it should. It's not as clean as I would like from my perspective, but I'd rather have Excel close completely than have a clean program. Thanks for all of the input.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: How to truly close Excel
« Reply #10 on: March 17, 2020, 12:49:28 PM »
This is what I use.
I think the catch is to also close the Workbooks before attempting to quit excel app.

Code - Auto/Visual Lisp: [Select]
  1.   (and
  2.     (setq file (getfiled "Open excel file" (getvar 'dwgprefix) "xlsx;xls" 2))
  3.     (cond
  4.       ((setq excel (vlax-get-object "Excel.Application")))
  5.       ((setq excel (vlax-create-object "Excel.Application")) (setq new_app T))
  6.     )
  7.  
  8.     (setq wbs (vlax-get-property  excel "workbooks"))
  9.  
  10.     (cond
  11.      ((progn
  12.         (setq wb (vl-catch-all-apply
  13.                    'vlax-get-property
  14.                      (list
  15.                       wbs
  16.                       "item"
  17.                       (strcat (vl-filename-base file) (vl-filename-extension file))
  18.                      )
  19.                   )
  20.        )
  21.        (if (vl-catch-all-error-p wb)
  22.          (setq wb nil)
  23.          wb
  24.        )
  25.       )
  26.      )
  27.      ((setq wb (vlax-invoke wbs 'open file)) (setq new_file T))
  28.     )
  29.   )
  30.   (progn
  31.    ...do your stuff...
  32.   )
  33. )
  34.  
  35.   new_app
  36.   (progn
  37.     (vlax-invoke-method wb   "close")
  38.     (vlax-invoke-method wbs  "close")
  39.     (vlax-invoke-method excel "quit")
  40.   )
  41.   (if
  42.     new_file
  43.     (vlax-invoke-method wb  "close")
  44.   )
  45. )
  46.  
  47. (mapcar '(lambda (x) (vl-catch-all-apply 'vlax-release-object (list x))) (list excel wbs wb))