如何使用 Windows 脚本关闭打开的文件?

发布于 2024-10-01 02:02:43 字数 143 浏览 5 评论 0原文

我想设置一个脚本作为 Windows 计划任务每​​天运行。该脚本应打开一个 Excel 电子表格,等待其运行宏(该宏设置为在每次打开电子表格时运行),然后在宏完成保存更改后关闭文件。宏本身的运行时间不会很长。如果我的脚本可以在再次关闭文件之前等待大约一分钟,那就足够了。

I want to set up a script to be run daily as a Windows Scheduled Task. The script should open an Excel spreadsheet, wait for it to run a macro (which is set up to run every time the spreadsheet is opened), and then close the file after the macro finishes saving the changes. The macro itself won't take very long to run. If my script can just wait for about a minute before closing the file again, that should be sufficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-10-08 02:02:43

完全未经测试的代码,大部分是从内存中编写的,但希望足以让您开始:

Set app = GetObject(, "Excel.application") ' assumes Excel is already running, otherwise use Set app = CreateObject("Excel.Application")
app.Visible = True ' useful while testing
app.run "MyMacro", "Param1" ' run the MyMacro macro with a string parameter "Param1"
WScript.Sleep(60000) ' wait 60,000 ms = 1 minute
app.ActiveDocument.Close
app.Quit
Set app = Nothing

Completely untested code and mostly written from memory but hopefully enough to get you started:

Set app = GetObject(, "Excel.application") ' assumes Excel is already running, otherwise use Set app = CreateObject("Excel.Application")
app.Visible = True ' useful while testing
app.run "MyMacro", "Param1" ' run the MyMacro macro with a string parameter "Param1"
WScript.Sleep(60000) ' wait 60,000 ms = 1 minute
app.ActiveDocument.Close
app.Quit
Set app = Nothing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文