Henrik K
Henrik K

Reputation: 1061

Powershell interact with open Excel

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?

Upvotes: 1

Views: 1660

Answers (1)

Keith Hill
Keith Hill

Reputation: 201652

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

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

Upvotes: 4

Related Questions