vba 如何通过计划任务运行 Excel 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8914703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:26:55  来源:igfitidea点击:

How can you run an Excel macro through a schedule task

excel-vbavbaexcel

提问by Bsquare

I need to run a macro in an Excel spreadsheet through a schedule task. I've defined the job as below:

我需要通过计划任务在 Excel 电子表格中运行宏。我已将工作定义如下:

"C:\Program Files\Microsoft Office\Office10\EXCEL.EXE" ""

"C:\Program Files\Microsoft Office\Office10\EXCEL.EXE" ""

The problem is that when the job starts Excel displays a pop-up and asks if we want to disable/enable the macros, which blocks the execution of the job.

问题是当作业启动时,Excel 会显示一个弹出窗口并询问我们是否要禁用/启用宏,这会阻止作业的执行。

How can I run this macro without having to manually click on enable macro?

如何在无需手动单击启用宏的情况下运行此宏?

采纳答案by Tim Williams

Write a short vbscript which launches Excel, loads your workbook and uses Application.Run to run the macro. Have your scheduled task run the vbscript.

编写一个简短的 vbscript 来启动 Excel,加载您的工作簿并使用 Application.Run 来运行宏。让您的计划任务运行 vbscript。

Eg: http://www.mrexcel.com/forum/showthread.php?t=302970

例如:http://www.mrexcel.com/forum/showthread.php?t= 302970

回答by Taptronic

You do not specify what version of Excel or what version of Windows (or Macintosh) so I will specify that I am using Excel 2010 from Office 32-bit on Windows 7 Ultimate 64-bit...

您没有指定什么版本的 Excel 或什么版本的 Windows(或 Macintosh),所以我将指定我在 Windows 7 Ultimate 64 位上使用 Office 32 位的 Excel 2010...



If you want to keep your workbook open and run code at a specified time, later on, then try this..

如果你想保持你的工作簿打开并在指定的时间运行代码,然后试试这个..

Insert a new code module to your project -or- use one that you already have in your project.

将新的代码模块插入到您的项目中 - 或者 - 使用您项目中已有的代码模块。

Create a scheduler subroutine as shown below:

创建一个调度程序子程序,如下所示:

Sub Scheduler()

'-- RUNS SUB(S) (OR FUNCTIONS) AT TIME SCHEDULED.

    Application.OnTime TimeValue("11:46:40"), "TheScheduledSub"

End Sub

Create a subroutine (or function) that you want to run. Here is one for this example:

创建要运行的子例程(或函数)。这是此示例的一个:

Sub TheScheduledSub()

    Debug.Print "TheScheduledSub() has run at " & Time

End Sub

Scheduler()will execute TheScheduledSub()at the exact time specified inside Scheduler(), defined by TimeValue. You can read more about TimeValuehere.

Scheduler()TheScheduledSub()在 内部指定的确切时间执行Scheduler(),由 定义TimeValue。您可以TimeValue在此处阅读更多信息。

You could probably pass the time value you want the Scheduler() sub to execute your sub(s) or function(s) in as a parameter in the sub.

您可能会传递您希望 Scheduler() 子程序执行子程序或函数的时间值作为子程序中的参数。

You could probably run a sub 5 hours from now, lets say. I.E. Application.OnTime Now + TimeValue("05:00:00"), "TheScheduledSub"

比方说,从现在开始,您可能可以运行 5 小时以下。IE Application.OnTime Now + TimeValue("05:00:00"), "TheScheduledSub"

or maybe a day from now? Play around with it.

或者也许一天后?玩弄它。

I did get two subs to execute at different times. I also tried to execute two at the same time. My experience with two different subs executing at the same time was that the second sub seemed to execute before the first, but I havent fully explored it. So YMMV.

我确实让两个潜艇在不同的时间执行。我也尝试同时执行两个。我对同时执行两个不同 sub 的经验是,第二个 sub 似乎在第一个之前执行,但我还没有完全探索它。所以YMMV。

I do not think that Excel VBA is multithread capable so keep that in mind. I am running the 32-bit Excel 2010 from the Office suite. Not sure how the 64-bit Office Excel behaves or for that matter any other version or platform. Anyway, multithreading / multitasking Excel VBA is not what you asked but it is to be considered.

我不认为 Excel VBA 具有多线程能力,所以请记住这一点。我正在从 Office 套件运行 32 位 Excel 2010。不确定 64 位 Office Excel 的行为方式或任何其他版本或平台。无论如何,多线程/多任务 Excel VBA 不是您所要求的,但需要考虑。

Seeing this question and playing with the above I can think of a few ideas I want to try myself!

看到这个问题,玩弄上面的我能想到几个我想自己尝试的想法!

回答by Bryan

You can also use workbook open and call macro. Just have scheduled tasks open the workbook and the macro will run when workbook is opened.

您还可以使用工作簿打开和调用宏。只需让计划任务打开工作簿,宏就会在工作簿打开时运行。

回答by Tan Piyapat

Also, I found it's a good idea to have

此外,我发现这是一个好主意

Application.DisplayAlerts = False

Application.DisplayAlerts = False

at the beginning to disable any popup message that could interrupt your macro when running.

在开始时禁用任何可能在运行时中断宏的弹出消息。

Or see tutorial video: http://youtu.be/lFSgV2gym9g

或者看教程视频:http: //youtu.be/lFSgV2gym9g

回答by dry

You can try to lower the security level of excel in order to avoid this pop up when launching excel.

您可以尝试降低excel的安全级别,以避免在启动excel时出现这种弹出窗口。