Board logo

标题: [文件操作] [分享]批处理调用PowerShell有选择的关闭Excel文件 [打印本页]

作者: Batcher    时间: 2022-8-31 19:23     标题: [分享]批处理调用PowerShell有选择的关闭Excel文件

【问题描述】

Excel同时打开了多个文档:
1.xlsx
2.xlsx
3.xlsx
...
任务管理器里面只能看到一个 excel.exe 进程
如果使用脚本关闭其它Excel文件,只保留3.xlsx不关闭?
作者: Batcher    时间: 2022-8-31 19:23

【解决方案】

test.bat
  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&goto :eof
  2. $NotClose = "3.xlsx"
  3. $Excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
  4. ForEach ($Wkb in $Excel.Workbooks) {
  5.     If ($Wkb.Name -ne $NotClose) {
  6.         $Wkb.Close()
  7.     }
  8. }
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2