0
votes

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.

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. 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.

I have, however, encountered two problems with the execution of the VBScript. First, is that it often fails to start when run by Task Scheduler. Second, is that when it does run, the Application.OnTime command within the macro doesn't work. 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?

VBA Timer Code:

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. 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.

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
2
Please provide the code of both the VBA and VBScript parts. Like it is stated now, there is not much that can be provided as answer.trincot

2 Answers

0
votes

Could you do it in a different way? 1) Run vbscript it in Windows Scheduler say every 1 minute or 1 hour or ….

2) Put some validation inside VBscript
If Condition then Execute your code Else Stop vbscript End if

Using this you probably can get rid of OnTime event handler

Best regards Alex

0
votes

I think you are over complicating things. You just need a Workbook_Open event and 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.

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