+ Reply to Thread
Results 1 to 11 of 11

Scheduling A Macro To Run Via Task Scheduler

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    133

    Scheduling A Macro To Run Via Task Scheduler

    I’m trying to get a macro to run each week via the task scheduler and I’ve never done this before. I googled it and found some code, but some of it wouldn’t allow me to get past the security halt for executing a macro. At least this code doesn’t do that, but it only pulls up the workbook and I never see any execution of the macro. I hoping someone here can help.

    The workbook is this case is named Acronis Backup check.xlsm and the macro inside it is Backup_Check. I created a folder and put the workbook and a run.bat and a script.vbs file in there, and am trying to execute the run.bat file. As I said it’ll put up the workbook but nothing ever happens with the execution of the macro. Then if I close the open workbook Excel crashes and restarts.

    Here’s the code in script.vbs:
    Dim args, objExcel
    
    Set args = wscript.Arguments
    Set objExcel = CreateObject("Excel.Application")
    
    objExcel.Workbooks.Open args(0)
    objExcel.Visible = True
    
    objExcel.Run "Acronis Backup Check!Backup_Check"
    
    objExcel.Activeworkbook.Save
    objExcel.Activeworkbook.Close(0)
    objExcel.Activeworkbook.Quit
    Here’s the code in run.bat:
    cscript script.vbs "C:\_Excel Macro Scripts\Acronis Backup Check.xlsm"
    Any idea how I can get a macro to run via scripting so I can execute it via the task scheduler?

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,398

    Re: Scheduling A Macro To Run Via Task Scheduler

    .
    .
    I can't make any comments about the VB script of BAT file ... however,
    in your workbook :

    In the ThisWorkbook module do you have a call to the macro when the workbook opens ?

    Option Explicit
    
    Private Sub Workbook_Open()
        Backup_Check
    End Sub

  3. #3
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    133

    Re: Scheduling A Macro To Run Via Task Scheduler

    Quote Originally Posted by Logit View Post
    .
    .
    I can't make any comments about the VB script of BAT file ... however,
    in your workbook :

    In the ThisWorkbook module do you have a call to the macro when the workbook opens ?

    Option Explicit
    
    Private Sub Workbook_Open()
        Backup_Check
    End Sub
    No, I don't. Should I? I thought that the statement:
    objExcel.Run "Acronis Backup Check!Backup_Check"
    would do that, running that macro. Is that not true?

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Scheduling A Macro To Run Via Task Scheduler

    As you are creating the Excel Object there is no need to include the workbook name, there's only 1 workbook open so just pass "Backup_Check" as the parameter - works fine for me.

    But...

    A Script file knows nothing about what is happening in Excel - it will continue and close Excel while the VBA code is running. You need to work around that.

    For example, creating a test workbook with a simple procedure to show a userform
    Sub Test
    UserFrom1.Show
    End Sub
    will work. But if the procedure is changed to show the userform modelessly, then the script errors...
    Last edited by cytop; 05-06-2017 at 10:52 AM.

  5. #5
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    133

    Re: Scheduling A Macro To Run Via Task Scheduler

    Quote Originally Posted by cytop View Post
    As you are creating the Excel Object there is no need to include the workbook name, there's only 1 workbook open so just pass "Backup_Check" as the parameter - works fine for me.

    But...

    A Script file knows nothing about what is happening in Excel - it will continue and close Excel while the VBA code is running. You need to work around that.

    For example, creating a test workbook with a simple procedure to show a userform
    Sub Test
    UserFrom1.Show
    End Sub
    will work. But if the procedure is changed to show the userform modelessly, then the script errors...
    Well, I'm new at all this so I guess I really don't understand, other than getting the fact that the script knows nothing about what Excel is doing after it executes the macro. I added this to my macro although I really don't understand what it does:

    UserForm1.Show
    Then I added a one minute wait after the macro was invoked to give it time before the script closes it, so now the script looks like this:
    Dim args, objExcel
    
    Set args = wscript.Arguments
    Set objExcel = CreateObject("Excel.Application")
    
    objExcel.Workbooks.Open args(0)
    objExcel.Visible = True
    
    objExcel.Run "Backup_Check"
    WScript.Sleep(60*1000)  'Sleep for 1 Minute
    
    objExcel.Activeworkbook.Save
    objExcel.Activeworkbook.Close(0)
    objExcel.Activeworkbook.Quit
    But still nothing happens at all. It just opens the workbook and sits there as far as I can tell. And when I close it minutes later Excel still crashes and restarts.

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Scheduling A Macro To Run Via Task Scheduler

    Showing a userform modally is a 'blocking' action. No other code can execute until the userform is unloaded so the procedure in Excel that loads the userform never continues until the userform is unloaded. That stops your script continuing and closing the workbook.

    If the userform is shown modelessly then the code continues after the userform is loaded, control is returned back to the script which then shuts down Excel, and pops an error.

    The attached shows both (you'll need to rename the TXT file as VBS - can't attah script files). First the userform will be displayed modally, the script will be paused until you click the Close button on the userform. It'll then show the userform modelessly. You'll note the msgbox from the script saying "Back from modeless test" (or something like that) will be displayed over the userform. The script has continued to execute after the userform is loaded. Click 'OK' on that message and the script crashes...
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    133

    Re: Scheduling A Macro To Run Via Task Scheduler

    Quote Originally Posted by cytop View Post
    Showing a userform modally is a 'blocking' action. No other code can execute until the userform is unloaded so the procedure in Excel that loads the userform never continues until the userform is unloaded. That stops your script continuing and closing the workbook.

    If the userform is shown modelessly then the code continues after the userform is loaded, control is returned back to the script which then shuts down Excel, and pops an error.

    The attached shows both (you'll need to rename the TXT file as VBS - can't attah script files). First the userform will be displayed modally, the script will be paused until you click the Close button on the userform. It'll then show the userform modelessly. You'll note the msgbox from the script saying "Back from modeless test" (or something like that) will be displayed over the userform. The script has continued to execute after the userform is loaded. Click 'OK' on that message and the script crashes...
    Cytop: Thanks, and I realize I'm likely being dense here, but please help me out. If I execute your code you sent it'll do just what you said it would. But I'm not sure how I should add that to my existing macro to get it run as a scheduled task. I don't want to display a form that requires user interaction from the scheduled task to continue, right? Using the form with a close button would do that I would think. And if the goal is to not return to the script where it saves and closes the workbook and quits before the macro has executed, wouldn't the Sleep command in the script, before the close, do that as well?

    Can you help me understand what code I should add to my existing macro, or the script, to get it to run? Like I said, I'm likely just no understanding things right. - Joel

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Scheduling A Macro To Run Via Task Scheduler

    The absolute simplest thing to do is load th euserform in he workbook open event and then call your procedure from the userform Activate event, if possible. Then you can leave the userform displayed on screen displaying little bits of info as to progress and simply unload it when the procedure finishes. Saves a lot of complication in the script.

    Above offered knowing absolutely nothing about the procedure; I'm like a little bit of electricity - always looking for the easiest path through

  9. #9
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    133

    Re: Scheduling A Macro To Run Via Task Scheduler

    Quote Originally Posted by cytop View Post
    The absolute simplest thing to do is load th euserform in he workbook open event and then call your procedure from the userform Activate event, if possible. Then you can leave the userform displayed on screen displaying little bits of info as to progress and simply unload it when the procedure finishes. Saves a lot of complication in the script.

    Above offered knowing absolutely nothing about the procedure; I'm like a little bit of electricity - always looking for the easiest path through
    Let me attach my macro, since I'm not sure I follow that exactly. Thanks again.
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Scheduling A Macro To Run Via Task Scheduler

    OK - it's not a 5 minute jobbie to go through that but 1 question arises first.

    In the procedure BackUp_Check is the line
                Workbooks.Open ("C:\_My Files\_My OCCF Files\Acronis Backup Check.xlsx")
    That is actually the name of the workbook you attached. Regardless if it's a separate copy, Excel will not open 2 workbooks with the same name even it they exist in different directories. Does that line ever get executed?

    Also, there is a total lack of any error handling. While that's probably not a bad thing while working on this it's not a good idea for a 'production release'.

  11. #11
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    133

    Re: Scheduling A Macro To Run Via Task Scheduler

    Quote Originally Posted by cytop View Post
    OK - it's not a 5 minute jobbie to go through that but 1 question arises first.

    In the procedure BackUp_Check is the line
                Workbooks.Open ("C:\_My Files\_My OCCF Files\Acronis Backup Check.xlsx")
    That is actually the name of the workbook you attached. Regardless if it's a separate copy, Excel will not open 2 workbooks with the same name even it they exist in different directories. Does that line ever get executed?

    Also, there is a total lack of any error handling. While that's probably not a bad thing while working on this it's not a good idea for a 'production release'.
    Oops. That one line can be commented out. This version of the macro assumed the macro was being run from outside the workbook, but since the bat file now actually opens it that line isn't needed.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help with Task scheduler to run a Macro
    By Lycn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2015, 03:29 PM
  2. [SOLVED] Run Macro with Windows Task scheduler
    By scottatbuckman in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-29-2014, 12:05 PM
  3. Running Excel from task scheduler. Scheduler doesn't end
    By tony h in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-31-2013, 09:49 PM
  4. Windows Task Scheduler, Run Macro.
    By JapanDave in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-03-2013, 03:02 PM
  5. Run Excel and macro using Windows Task Scheduler
    By MilroyD in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2013, 11:24 PM
  6. windows task scheduler VBS script to run macro
    By els050676 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-16-2013, 09:13 AM
  7. Connecting VBA macro to OS Task Scheduler
    By vierx2007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2010, 02:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1