如何连接到从现有的PowerShell的Excel实例? [英] How to connect to existing instance of Excel from PowerShell?
问题描述
这是通过PowerShell的自动化Excel的所有例子开始这一行:
All examples that automate Excel through PowerShell start with this line:
PS> $Excel = New-Object -Com Excel.Application
这似乎在处理Excel的一个新的实例,例如,运行 $ Excel.Visiable = $真实
将显示一个空的,空白的Excel窗口,而不是切换到现有的工作簿。
This seems to be handling a new instance of Excel, e.g. running $Excel.Visiable = $true
will show an empty, blank Excel window, not switch to the existing workbook.
如果已经有运行Excel的一个实例,有没有连接到它的方式?
推荐答案
而不是通常的新对象
-ComObject
excel.application我们这个
Instead of the usual New-Object
-ComObject
excel.application us this
$的Excel = [Runtime.Interopservices.Marshal] :: GetActiveObject('Excel.Application')
其余部分保持不变。
一个缺点。您只有通过将启动PS1相同用户启动Excel的实例。
One downside. You will only get the excel "instances" started by the same user that will initiate the ps1.
这篇关于如何连接到从现有的PowerShell的Excel实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!