在特定时间运行VBA代码 [英] Running VBA code at a certain Time

查看:168
本文介绍了在特定时间运行VBA代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试运行VBA程序,每天晚上9点发送电子邮件。如何实现。


我不确定这是否有用,但代码如下

展开 | 选择 | Wrap | 行号

解决方案

我还没有对此进行过测试,但是:

  1. 复制并粘贴代码到名为fXptHoRpt的公共函数。
  2. 创建一个名为AutoExec的宏,它将执行此函数(RunCode())。
  3. AutoExec宏的下一行将退出宏,因此关闭数据库。
  4. 创建一个预定的Tas你的电脑上的k将在晚上9点开始每天。假设您的数据库名为Test.mdb并驻留在C:\ Test Directory中,此任务的命令行将类似于:

    展开 | 选择 | Wrap | 行号

我喜欢ADezii的方法。


另一种方法是拥有一个隐藏的表单,让它的计时器每分钟检查一次,以查看当前时间。如果时间与晚上9点匹配,则执行宏。这要求数据库始终正常运行。


@Megalog


我喜欢ADezii''方法。



是的,但实际上会有效吗? (大声笑)。我主要担心的是,由于代码将以异步方式运行,因此在发送电子邮件/附件的功能代码尚未完成时,可以执行退出宏操作。但有趣的情况。在函数调用之后,但在退出命令之前,某些延迟方法实际上可能会起作用。


Hi,
I am trying to run a VBA procedure to send an e-mail every day at 9:00 PM. How would this be achieved.

I''m not sure if this would help but the code is as follows

Expand|Select|Wrap|Line Numbers

解决方案

I haven''t actually tested this, but:

  1. Copy and Paste your code to a Public Function called fXptHoRpt.
  2. Create a Macro named AutoExec that will execute this Function (RunCode()).
  3. The next line of the AutoExec Macro will Quit the Macro, thus closing the Database.
  4. Create a Scheduled Task on your PC to start at 9:00 P.M. every day. Assuming your Database is named Test.mdb and resides in the C:\Test Directory, the Command Line for this Task will be something similar to:

    Expand|Select|Wrap|Line Numbers


I like ADezii''s approach.

The alternative would be to have a hidden form, that has it''s timer checking every minute to see what the current time is. If the time matches 9:00 pm, then it executes the macro. This requires the database always be up and running though.


@Megalog

I like ADezii''s approach.

Yea, but will it actually work? (LOL). My main concern here is that since the code will be running asynchronously, the Quit Macro Action may execute while the Function Code sending the E-Mail/Attachments has not yet completed. Interesting scenario, though. Some Delay Method, after the Function Call, but prior to the Quit Command, may actually do the trick.


这篇关于在特定时间运行VBA代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆