简体   繁体   English

如何用VBS关闭所有打开的Excel工作簿

[英]How to close all open Excel workbooks with VBS

I automate a process using a.vbs file and sometimes I forget to close the excel file at night, so when the process runs the file opens as a read-only causing issues.我使用 a.vbs 文件自动化进程,有时我忘记在晚上关闭 excel 文件,因此当进程运行时,文件以只读方式打开,导致问题。 I'd like to just close or end all instances of open workbooks.我只想关闭或结束打开的工作簿的所有实例。

I thought a ".Quit" would do the trick but doesnt seems to close workbooks that are currently open.我认为“.Quit”可以解决问题,但似乎无法关闭当前打开的工作簿。 I test.quit in a macro and it does indeed close all open workbooks and the excel app in general.我在宏中测试退出,它确实关闭了所有打开的工作簿和一般的 excel 应用程序。

Any ideas?有任何想法吗?

Set ExcelApp = CreateObject("Excel.Application")

ExcelApp.Applicaiton.Quit

'reopen actual file as non read only

Set ExcelApp = CreateObject("Excel.Application")

ExcelApp.Visible = True

ExcelApp.DisplayAlerts = False

ExcelFilePath = "C:\Users\user\Documents\test.xlsm"
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)

Instead of代替

Set ExcelApp = CreateObject("Excel.Application")

try尝试

Set ExcelApp = GetObject(, "Excel.Application")

To bind to existing excel instance.绑定到现有的 excel 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM