vba - 如何使用 Powershell 在打开的 Excel 工作簿上运行 vba 程序
问题描述
我正在构建一个 powershell 脚本来搜索指定打开的 excel 工作簿并在其上运行 vba。
当有一个带有多个工作簿的 excel 应用程序时,它运行良好。
但是,当有多个 excel 应用程序时,它不起作用,因为代码仅在一个 excel 应用程序上搜索工作簿。如何在所有打开的 Excel 应用程序上搜索工作簿?
这是我正在使用的代码
Try{
# Check if any excel application is running
$ExistingExcel = ((get-process excel | select MainWindowTitle, ID, StartTime |
Sort StartTime)).mainwindowtitle
# Get existing excel application
$excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
# Get a list of workbook name in the excel application
$ExcelList = $excel.workbooks | Foreach-object {$_.name}
# Check if the list of workbooks contain the target workbook
$targetWb = if ($ExcelList -contains "MyTargetWorkbook.xlsm")
{
# Activate the target workbook and run the vba
$excel.Workbooks.Item("MyTargetWorkbook.xlsm")
$targetwb.Activate()
$excel.run('MyVbaProgram')
} else {
# Do error handling
}
}Catch{
# Do error handling
}
解决方案
推荐阅读
- flutter - Flutter 应用程序 - 想要在不被按下的情况下播放声音 oppressed()
- java - 了解链表中的插入
- linux - pid mem和tid mem的区别
- java - 如何在 Apache Beam 中使用 KafkaIO 指定 kafka 代理
- python - 如何从公司名称中提取电子邮件 ID
- python - 将来自多个 Python 进程的视频帧同时写入单个文件
- node.js - 存档器与 PassThrough() 组合/管道,追加和完成不是一个函数 - Nodejs Stream
- rust - 重构 Rust 中的方法而不会导致“不能借用自我”编译错误
- swift - 如何对来自 firebase 的子值求和并存储在标签中。Firebase-Swift
- autodesk-forge - 为什么我没有得到清单中的所有数据?