views:

42

answers:

1

To interact with excel in Powershell it is common to start a new excel as follows:

$x = New-Object -comobject Excel.Application

Instead of that I have an open Excel process already. (I get it as follows)

$excelprocess = Get-Process | Where-Object {$_.name -eq "excel"} | Sort-Object -Property "Starttime" -descending | Select-Object -First 1

Is there a way to interact with this specific excel process over PS?

+1  A: 

You want to use the Marshal.GetActiveObject method e.g.:

$xl = [Runtime.InteropServices.Marshal]::GetActiveObject('Excel.Application')
$xl.Quit()
Keith Hill