How to connect to existing instance of Excel from PowerShell?

前端 未结 2 1880
既然无缘
既然无缘 2020-12-07 02:44

All examples that automate Excel through PowerShell start with this line:

PS> $Excel = New-Object -Com Excel.Application

This seems to b

相关标签:
2条回答
  • 2020-12-07 02:55

    Instead of the usual New-Object -ComObject excel.application us this

    $excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')

    Rest stays the same.

    One downside. You will only get the excel "instances" started by the same user that will initiate the ps1.

    0 讨论(0)
  • 2020-12-07 03:09

    Yes, you can access the COM object via HWND [Window handle] using this WIN32 API (AccessibleObjectFromWindow).

    (See a SO post sample here of using this api via C#)

    .

    You may have to write an assembly in C# and/or manipulate P/Invoke calls via Powershell.

    You may give a shot at it & see how it goes.

    0 讨论(0)
提交回复
热议问题