Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Excel remains running

$
0
0

Marleen,
This is a code segment that shows what I use for the Excel not closing problem. It happens I am just automating saving the .XLSX file as .CSV. What you would want is the first line getting the list of processes at the start of Excel automation and the last line after the Excel automation that kill all Excel processes that are still running that were not in the first list. It assumes no Excel process starts in the meantime that you care about–that is a safe assumption in my case.
$runningExcelProcesses = @(Get-Process -Name excel -ErrorAction SilentlyContinue | Select-Object ID)
$runningExcelProcesses
$xls=”T:somefilepath.xlsx”
$csv=”T:someotherfilepath.csv”
# You can use the Excel COM object in PowerShell to save the file as a CSV file. http://www.powershellcommunity.org/Forums/tabid/54/aft/657/Default.aspx
# $xlCSV is a constant for the SaveAs method that tells Excel to save the file in the CSV format.
# xlWorkbookDefault = 51 http://www.excel-answers.com/microsoft/Excel-Programming/34263208/fileformat-list.aspx and http://www.pcreview.co.uk/forums/use-automation-save-xls-into-pdf-office-2007-a-t3042211.html
new-variable xlCSV 6 -option Constant -ErrorAction SilentlyContinue # $xlCSV=6
new-variable xlWorkbookDefault 51 -option Constant -ErrorAction SilentlyContinue # $xlWorkbookDefault=51
$xl=New-Object -com “Excel.Application”
$wb=$xl.workbooks.open($xls)
$wb.SaveAs($csv,$xlCSV) # create a CSV version of the file
$xl.displayalerts=$False
$xl.quit()
# One thing that seems to be a problem though is that even though you quit Excel, the Excel process doesn’t really terminate so you may also need kill it
# if (ps excel) { kill -name excel}
Get-Process -Name excel -ErrorAction SilentlyContinue | Select-Object ID | Where-Object {$runningExcelProcesses.ID -notcontains $_.ID} | Stop-Process

Steve


Viewing all articles
Browse latest Browse all 13067

Trending Articles