Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

No way to close Excel. Any reason ?

1,498 views
Skip to first unread message

IT Staff

unread,
Dec 16, 2008, 3:31:48 AM12/16/08
to

I've a excel spreadsheet that contains computer data on 1st column. AFter
reading all rows and exit, the Excel.exe was not removed from the taskbar.

===================================================================
Add-PsSnapin Quest.ActiveRoles.ADManagement


function release-ref($ref)
{
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ref)
[system.gc]::collect()
[system.gc]::WaitForPendingFinalizers()
}


$xl = new-object -comobject excel.application
$xl.Visible = $false
$filename = "c.xls"

$wb = $xl.Workbooks.open("d:\$filename")

$ws = $wb.worksheets.item(1)

$row = 1
while ($true)
{
$computername = $ws.Cells.Item($row,1).text

if ($computername.length -eq 0) {exit} else {get-qadcomputer
$computername" }
$row++
}

## below does not remove excel.exe ##
release-ref $ws
release-ref $wb
$xl.quit()
release-ref $xl
Remove-Variable xl

## below does not remove excel.exe ##
$obj = gwmi -computer . -query "select * from win32_process where
name='excel.exe'"
$obj
$obj.terminate()

## below does not remove excel.exe ##
get-process EXCEL | stop-process


OldDog

unread,
Dec 16, 2008, 9:39:05 AM12/16/08
to

Alex, over at the Power Shell group in Google talked about Excel not
shutting down when told.

Excel is generally the worst offender this way - there have been
problems
with it not shutting down when told to that go back a very long time -
but
there definitely are other ActiveX applications that exhibit this kind
of
behavior. Since ActiveX applications are generally rare and each have
their
own quirks, I usually test whether they shut down in different
situations
when using them from PowerShell by keeping an eye on the process list,
then
release the object if they don't shut down correctly.
Unfortunately, this isn't all that useful if you're keeping an
application
around to use during a PowerShell session. In those cases, I've
noticed that
even some of the better-behaved ActiveX applications don't work the
way we
might want. For example, some of the better-behaved ActiveX
applications
will indeed shut down automatically if they don't have an open,
modified
document when you close PowerShell by typing exit, but will hang
around if
you close PS with the "X" at the top of the shell window. My general
rule of
thumb is to always unhide ActiveX applications and minimize them. This
at
least ensures that I can see and close them if I exit PS the wrong way
or
lose connection to the app somehow.

One way in Power Shell to kill the process is;

Stop-Process -name EXCEL

Another is to try to release the com object after quiting:

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)

Or you can just add these two lines to the end of your script:

$xl.quit()
spps -n excel

Marco Shaw [MVP]

unread,
Dec 16, 2008, 3:40:18 PM12/16/08
to
IT Staff wrote:
> I've a excel spreadsheet that contains computer data on 1st column. AFter
> reading all rows and exit, the Excel.exe was not removed from the taskbar.
>
> ===================================================================
> Add-PsSnapin Quest.ActiveRoles.ADManagement
>
>
> function release-ref($ref)
> {
> [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ref)
> [system.gc]::collect()
> [system.gc]::WaitForPendingFinalizers()
> }
>
>
> $xl = new-object -comobject excel.application
> $xl.Visible = $false

If instead, you try making it visible, are you prompted in any way on
the screen/console?

What version of Excel?

Marco

--
*Microsoft MVP - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition (due December
15th, 2008)
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com

IT Staff

unread,
Dec 16, 2008, 8:12:14 PM12/16/08
to
Even if excel 2003 (office 2003) is *visible*, it does not close.

Note that i run the script from command prompt.


Marco Shaw [MVP]

unread,
Dec 16, 2008, 9:50:02 PM12/16/08
to
IT Staff wrote:
> Even if excel 2003 (office 2003) is *visible*, it does not close.
>
> Note that i run the script from command prompt.
>
>

But you're not getting any kind of prompt right? I still want to make
100% sure...

Try something much simpler like just opening and then closing Excel to
see if you can use your function to release excel.exe.

IT Staff

unread,
Dec 17, 2008, 1:03:56 AM12/17/08
to
I perform this scenario

a) In powershell screen or command prompt executing the script. (unable to
close from task list from my codes)
b) run excel.exe manually which opens a typical excel workbook. (closing it
remove it from task list)

When both above are running, i ran a command prompt

tlist "excel.exe" | find /i "excel.exe", results shown below

5276 EXCEL.EXE
CmdLine: "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE"
/automation -Embedding (this is the powershell call)
0x30000000 EXCEL.EXE
5080 EXCEL.EXE Microsoft Excel - Book1 (this is the normal excel
callup)
CmdLine: "C:\PROGRA~1\MICROS~2\OFFICE11\EXCEL.EXE"
0x30000000 EXCEL.EXE

Any ideas ?


gordo

unread,
Dec 30, 2008, 2:58:06 PM12/30/08
to
Works like a charm, thanks OldDog.

$obj.Quit()
spps -n excel


"OldDog" wrote:

> On Dec 16, 2:31 am, "IT Staff" <jkk...@hotmail.com> wrote:
> > I've a excel spreadsheet that contains computer data on 1st column. AFter

> > reading all rows and exit, the Excel.exe was not removed from the taskbar..

OldDog

unread,
Dec 30, 2008, 5:41:41 PM12/30/08
to
> > spps -n excel- Hide quoted text -
>
> - Show quoted text -

Always a pleasure

gordo

unread,
Dec 31, 2008, 4:06:02 PM12/31/08
to
Okay, it worked yesterday when I used it. With Task Manager open I could see
the EXCEL proc terminate. Today something else is going on. I've tried
rebooting the machine but no change.

EXCEL stays in Task Manager, but if I then manually type "spps -n excel" in
the open shell it works. I don't get it.

I call my script from the Run line: powershell.exe -noexit
c:\PStest\read.ps1, with the closing lines in the script
$xl.Quit()
spps -n excel

Everything in the script does as intended (thaks to your cookbook and blog!)
but it isn't until after I manually type the spps cmdlet that the process
ends.

What are your thoughts on this, and do you have any solution suggestions?

Thanks,
Gordo


> On Dec 30, 1:58 pm, gordo <go...@discussions.microsoft.com> wrote:
> > Works like a charm, thanks OldDog.
> >
> > $obj.Quit()
> > spps -n excel

> Always a pleasure

gordo

unread,
Dec 31, 2008, 5:33:01 PM12/31/08
to
Oops. A little knowledge is a dangerous thang.

I created the problem with this:
if ($Project.length -eq 0) {exit}
$xl.Quit()
spps -n excel


And corrected it with this:
if ($Project.length -eq 0)
{
$xl.Quit()
spps -n excel
exit

Message has been deleted

OldDog

unread,
Jan 1, 2009, 6:15:33 PM1/1/09
to
On Dec 31 2008, 4:33 pm, gordo <go...@discussions.microsoft.com>
wrote:

I take it that it's working now?

gordo

unread,
Jan 5, 2009, 12:41:01 PM1/5/09
to
The part I have so far is working now. Thanks for the resources you've
posted. They're incredibly helpful.

Now, I just need to get my array of arrays working :)

>
> I take it the it's working now?
>

0 new messages