简体   繁体   English

通过计时器通过vbscript自动运行宏

[英]Run macro automatically via vbscript from timer

I have a macro that downloads some information from the internet and I need to schedule it to run automatically starting at a certain time of day and running repeatedly both at set time intervals although occasionally at specific times output by the macro. 我有一个宏,可以从互联网上下载一些信息,我需要安排它从一天的某个时间开始自动运行,并以设定的时间间隔重复运行,尽管有时会在宏输出的特定时间重复运行。

I have approached the problem in two ways, both of which have presented problems that I can't figure out. 我以两种方式解决了这个问题,两种方式都提出了我无法解决的问题。 The first approach is using a timer sub that runs the macro using the Application.OnTime command with the runtime derived from a cell in the workbook that is updated by the macro. 第一种方法是使用计时器子,该子程序通过Application.OnTime命令运行宏,该运行时具有从工作簿中由该宏更新的单元格派生的运行时。

This method works pretty well if I am on the computer doing something else. 如果我在计算机上执行其他操作,则此方法效果很好。 However, I have noticed that if I leave, even if the computer doesn't go to sleep, the program inevitably stops re-running after 30min or so. 但是,我注意到,即使我离开计算机,即使计算机没有进入睡眠状态,该程序也不可避免地会在30分钟左右后停止重新运行。 Sometimes it also stops re-running even when I'm active on the computer. 有时即使我在计算机上处​​于活动状态,它也会停止重新运行。 Why is this? 为什么是这样? What could be causing this? 是什么原因造成的? Also, naturally, I can't schedule the program to open and run automatically from within the macro, so I turned to the VBScript & Task Scheduler method for this part of the solution. 同样,自然地,我无法安排程序从宏内部自动打开并运行,因此对于该部分解决方案,我转向了VBScript&Task Scheduler方法。

I have, however, encountered two problems with the execution of the VBScript. 但是,我在执行VBScript时遇到了两个问题。 First, is that it often fails to start when run by Task Scheduler. 首先,它是由Task Scheduler运行时通常无法启动的原因。 Second, is that when it does run, the Application.OnTime command within the macro doesn't work. 其次,是当它运行时,宏中的Application.OnTime命令不起作用。 This is a problem because the macro needs to run sometimes at irregular time intervals which are determined during the course of the day as the macro runs through its iterations. 这是一个问题,因为宏有时需要以不规则的时间间隔运行,该时间间隔是在一天的过程中确定的,因为该宏在其迭代过程中运行。 Why would the vbscript fail to run when task manager tries, and why would it not initiate the application.ontime command the way the macro does when I run it manually? 为什么当任务管理器尝试时vbscript无法运行,为什么它不像我手动运行宏时那样启动application.ontime命令?

VBA Timer Code: VBA计时器代码:

Sub Timer()
    Dim Runtime As Date

    If Time <= TimeValue("17:45:00") Then
        Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 10
    ElseIf Time > TimeValue("17:45:00") And Time <= TimeValue("18:15:00") Then
        Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 13
    ElseIf Time > TimeValue("18:15:00") And Time <= TimeValue("18:45:00") Then
        Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 16
    ElseIf Time > TimeValue("18:45:00") And Time <= TimeValue("19:15:00") Then
        Workbooks("10am_Swing_Report").Sheets("Parameters").Cells(17, 2).Value = 19
    End If

    Workbooks("10am_Swing_Report").Sheets("Parameters").Range("B16").Calculate
    If Time > Workbooks("10am_Swing_Report").Sheets("Parameters").Range("B16").Value Then
        Runtime = Time + TimeValue("00:00:30")
    Else
        Runtime = Workbooks("10am_Swing_Report").Sheets("Parameters").Range("B16").Value
    End If

    Application.OnTime TimeValue(Runtime), "Swingtimer"

    Workbooks("10am_Swing_Report").Sheets("Parameters").Range("C16").Value = Runtime
    Workbooks("10am_Swing_Report").Save
End Sub

Thanks! 谢谢!

I've updated the Vbs code to include a validation that the runtime has been passed, but I'm still encountering a problem with Task Scheduler failing to start the task. 我已经更新了Vbs代码,以包括已通过运行时的验证,但是我仍然遇到Task Scheduler无法启动任务的问题。 I'm getting the following error: "Launch request ignored, instance already running" followed by: "Task Start Failed". 我收到以下错误:“启动请求已忽略,实例已在运行”,其后是:“任务启动失败”。 I was originally thinking this was because the Vbscript was still running from the previous instance, but I'm getting this from the very beginning. 我原本以为这是因为Vbscript仍在以前的实例中运行,但是我从一开始就开始使用它。

Option Explicit


Dim xlApp       
Dim xlBook      
Dim runtime 
Dim xlSheet



Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
'xlApp.Application.DisplayAlerts = True
Set xlBook = xlApp.Workbooks.Open("C:\Users\DORIAN\Dropbox\Swing_Report.xlsm",1,False)
Set xlSheet = xlBook.Sheets("Parameters")

'xlBook.application.Visible = True

'MsgBox FormatDateTime(xlSheet.Range("B16").Value,3)
runtime = TimeValue(FormatDateTime(xlSheet.Range("B16").Value,3))
'MsgBox runtime


If Time > runtime Then 
    xlApp.Run "Swingtimer"
    'MsgBox "Running..."
    WScript.Sleep 30000
Else
xlApp.Application.OnTime runtime, "Swingtimer"
'MsgBox "Timer Set"
End If


xlbook.close True

WScript.Quit

Could you do it in a different way? 您能以其他方式做到吗? 1) Run vbscript it in Windows Scheduler say every 1 minute or 1 hour or …. 1)在Windows Scheduler中运行vbscript,例如每1分钟或1小时或…。

2) Put some validation inside VBscript 2)在VBscript中放入一些验证
If Condition then Execute your code Else Stop vbscript End if 如果条件,则执行您的代码,否则执行。

Using this you probably can get rid of OnTime event handler 使用此方法,您可以摆脱OnTime事件处理程序

Best regards Alex 最好的问候亚历克斯

I think you are over complicating things. 我认为您正在使事情复杂化。 You just need a Workbook_Open event and Windows Task Scheduler. 您只需要一个Workbook_Open事件和Windows Task Scheduler。

Something like this (customize to suit your needs). 这样的事情(可定制以满足您的需求)。

Private Sub Workbook_Open()
    Msgbox Date
    Worksheets("Sheet1").Range("A1").Value = Date
End Sub

Schedule the event using the Windows Task Scheduler. 使用Windows Task Scheduler安排事件。

http://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html http://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html

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

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