如何从 PowerShell 连接到现有的 Excel 实例? [英] How to connect to existing instance of Excel from PowerShell?

查看:29
本文介绍了如何从 PowerShell 连接到现有的 Excel 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有通过 PowerShell 自动化 Excel 的示例都以这一行开头:

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

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

这似乎是在处理一个新的 Excel 实例,例如运行 $Excel.Visiable = $true 将显示一个空的空白 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 实例在运行,有没有办法连接到它?

推荐答案

代替通常的 New-Object -ComObject excel.application us this

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆