Share via


Excel background process

Question

Tuesday, August 9, 2016 6:26 PM

I have been trying to do some work in Excel, and I ran into something for which I need some assistance. 

I am working on a 64-bit Windows 10 system but my company installs 32-bit Office.

I start Excel with PowerShell by using

$xl = New-Object -ComObject "Excel.Application"

It opens fine, but when watching Task Manager, I see a background process for Excel start up in addition to the App showing.  When I complete my processing, I close Excel with

$xl.Quit()

The App showing in Task Manager disappears, but the background process hangs around - I have to kill it within Task Manager.  If I run my script multiple times, I end up with all sorts of background processes for Excel.  If I launch Excel from the Windows menu/tile, it does not create that background process.

Is there some sort of test I should be doing in my PowerShell script to start Excel differently so I don't get the background process?  I have tried doing the following in PowerShell, but it doesn't seem to have any effect.

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

Thanks for educating me on what's happening. 

. : | : . : | : . tim

All replies (12)

Tuesday, August 9, 2016 8:03 PM ✅Answered

You are asking Excel to connect to a COM server.  When you have a reference to Excel it will not close down.  Removing the reference allows the connection to be released. 

\(ツ)_/


Tuesday, August 9, 2016 6:47 PM

If you know the name of the process, you can stop the process from your script:

Stop-Process -processname excelprocessnameTerry

Tuesday, August 9, 2016 7:48 PM

Yup.  That is the way Excel works.  You have to release all objects you have created before the Excel 'server" process exits.

\(ツ)_/


Tuesday, August 9, 2016 7:49 PM

See: https://technet.microsoft.com/en-us/library/ff730962.aspx

\(ツ)_/


Tuesday, August 9, 2016 7:50 PM

I understand that.  But my question is why it should be getting created in the first place.  When I launch Excel from the menu/tile, that process is not being created.  Why is it getting created when I launch Excel from PowerShell?  Am I not including some parameter I should be?  Or, do I need to include something on the Quit to get rid of it automatically?

. : | : . : | : . tim


Tuesday, August 9, 2016 8:58 PM

That was it, but it was a lot harder than simply deleting the Excel COM object.  I had to delete the workbook and worksheets.  I had created a couple of charts, and they had to be deleted. I had defined a range of cells to be used in creating the chart, and it had to be deleted.  I had defined a second series of variables for graphing, as I had created two graphs on a single chart, and it had to be deleted.  Evidently Excel creates all those as COM objects.  Not intuitively obvious.  I just kept trying additional values until it finally cleared up.

Thanks!  It got me what I needed.

. : | : . : | : . tim


Tuesday, August 9, 2016 8:59 PM

That is what I posted.  You have to remove every reference.

\(ツ)_/


Wednesday, August 10, 2016 10:18 PM

Is there a way to query what COM objects were created? I am having random issues with clearing them. Sometimes they clear fine, and other times some hang around. 

. : | : . : | : . tim


Wednesday, August 10, 2016 10:38 PM

The trick is to not create object references.  Remember that most things are refernces to already existing objects.  You don't need them

Example:

$xl = New-Object -ComObject Excel.Application
$wb = $xl.Workbooks.Open("$pwd\book1.xlsx")
$wb.Worksheets['sheet1'].UsedRange.Columns.Count

That creates only two references and gives you access to all aspects of any Workbook or sheet.

You can also reference:

$xl.ActiveSheet
$xl.ActiveWorkbook

etc.

\(ツ)_/


Thursday, August 11, 2016 2:47 PM

Good advice.  I can see how I can get rid of a couple objects that I will not have to delete - already got rid of one in trying out what you said.  But I am creating a spreadsheet and creating charts, so I will not be able to get down to two.  Maybe having fewer, though, will lessen the chance for not getting cleared.

I take it from your answer that there is no way to query COM objects owned by a process?

. : | : . : | : . tim


Thursday, August 11, 2016 2:50 PM

Create an array and add the new objects to the array as you create them.  Enumerate the array to release all created references.

\(ツ)_/


Thursday, August 11, 2016 3:07 PM

I was going to reply that I was going to create a similar hack.

Thanks for the assistance.

. : | : . : | : . tim