Running Excel Macro from Task Scheduler

jen4950

[H]F Junkie
Joined
Apr 25, 2001
Messages
11,380
I need to run an Excel VBA Macro from a batch file fired off by Task Scheduler, and it creates a BUNCH of new spreadsheets.

I've got a really big nightly project that is split between a bunch of Virtual Machines across a couple hosts. Excel works best for formatting and producing PDF's. It's really easy to control page formatting in Excel and get it to look right every time. And most of the work is tabular and mathematical with conditional formatting- it would be much more difficult to use anything other than Excel for the report generation.

The virtual machines are all on PCIe SSD arrays, so I'm doing all of the processing on the local storage, and then copying the final work back to the file server.

And the idea is with a pre-produced PDF, load time is sub 1 second, so when my staff needs the report for a quick decision with the client on the phone, they don't wait at all for processing. Data 1 day current is sufficient for this report, so it doesn't need to be real-time anyway- but there is a ton of processing to build the report.

The Batch File:

Code:
cd C:\DSR
ROBOCOPY "\\xxxxx005\System\Run_DSR.vbs" "C:\DSR\Run_DSR.vbs" /R:1 /W:1 /COPY:DATS
wscript.exe "C:\DSR\Run_DSR.vbs"
exit

The meat of the VBS Script also includes SMTP e-mail messaging to the IT Management group. I didn't include that here.

The nice thing is the .BAT file updates the VBS to the most current version, and the VBS updates the .BAT and .XLSM to the most current version, so it keeps it's self current without intervention.

But also by using a .BAT file set up with Task Scheduler, It's really easy to call all of the VM's from command line; and all of the Task XML files are the same and can be easily imported to new VM's, and you don't have to worry about getting the default scripting host right or anything like that- it just works:

Task XML Snippet:
Code:
<Actions Context="Author">
  <Exec>
     <Command>Run_VBS.bat</Command>
     <WorkingDirectory>C:\DSR\</WorkingDirectory>
  </Exec>
</Actions>

Run ALL Batch file, mainly used for testing off-hours:
Code:
SCHTASKS /RUN /S STV10001 /TN \DSRTask
SCHTASKS /RUN /S STV20001 /TN \DSRTask
SCHTASKS /RUN /S STV10002 /TN \DSRTask
SCHTASKS /RUN /S STV20002 /TN \DSRTask
...

The VBS Code then gets the most current XLSM, does some messaging, creates some log files, and creates an Excel Object. The it opens the XLSM file and runs the Macro:

Code:
Set wshShell = CreateObject("WScript.Shell")
 
' Run As Adminstrator
If WScript.Arguments.length = 0 Then
   Set ObjShell = CreateObject("Shell.Application")
   ObjShell.ShellExecute "wscript.exe", """" & _
   WScript.ScriptFullName & """" &_
   " RunAsAdministrator", , "runas", 1
   Wscript.Quit
End if
 
Set obj6 = CreateObject("Scripting.FileSystemObject")
obj6.CopyFile "\\xxxxx005\System\Run_VBS.bat","C:\DSR\Run_VBS.bat", True
Set obj6 = Nothing
...
E-Mail Start Notification and file cleanup
Logfile creation
...

And then later on, the Excel Call part:

Code:
Dim xlBook, ObjExcel
Set ObjExcel = CreateObject("Excel.Application")
 
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = "C:\DSR\DSR.xlsm"
Set oWorkBook = ObjExcel.Workbooks.Open(strWorkerWB)
on error resume next
  ' Run the calculation macro
  ObjExcel.Run "'C:\DSR\DSR.xlsm'!Module1.RunDSR"
  if err.number <> 0 Then
  ' Error occurred - just close it down.
  End If
  err.clear
on error goto 0
' Clean up and shut down

This then runs the RunDSR Macro in the .XLSM file. And I know it works because the log file writes all the way up to the first Workbook.Add call:

Code:
Log File Started.  - 4/11/2016 4:39:33 PM
SQL Import Routine Started.  - 4/11/2016 4:39:33 PM
Filesave - 3.0999999580672E-03
Data Acquisition - 5651.42770000011
 
...

And then it stops.

The whole routine goes all the way thru to completion when I run the Batch file manually as Administrator.

What I have read is that when Application.Excel is opened via OLE Automation- none of the Add-ins are started with it. So, when Workbook.Add is called, the underlying reference is not there.

Long story, short question-

How to I initiate that reference to get this to work automatically?

This has been totally eating my lunch, so ask away- I need to get this working...
 
Could you maybe have the batch file launch Excel first and then have the vbs just do what it needs using the already open Excel?

Then when it is done, you can always use taskkill to force it to close Excel.
 
Could you maybe have the batch file launch Excel first and then have the vbs just do what it needs using the already open Excel?

Then when it is done, you can always use taskkill to force it to close Excel.

This does that- problem is it does not loan the Add-ins or functionality to be able to create a new workbook, due to the VBA being called from OLE Automation.

I just need to figure out how to call that back in.

Code:
Set ObjExcel = CreateObject("Excel.Application")
 
This does that- problem is it does not loan the Add-ins or functionality to be able to create a new workbook, due to the VBA being called from OLE Automation.

I just need to figure out how to call that back in.

Code:
Set ObjExcel = CreateObject("Excel.Application")

Right... so depending on the version of Office, you can just launch Excel from the batch file... not from the vbs file... like so:
C:\Program Files (x86)\Microsoft Office\Office16\Excel.exe

That will launch Excel with the add-ins, etc.

Then you use the vbs file to find the pid for Excel and then interact through the fully launched Excel with the pid.

Then when done, close Excel.

Or you can manually load the add-ins as specified here:
Add-ins do not load when using the CreateObject command in Excel
 
Right... so depending on the version of Office, you can just launch Excel from the batch file... not from the vbs file... like so:
C:\Program Files (x86)\Microsoft Office\Office16\Excel.exe

That will launch Excel with the add-ins, etc.

Then you use the vbs file to find the pid for Excel and then interact through the fully launched Excel with the pid.

Then when done, close Excel.

Or you can manually load the add-ins as specified here:
Add-ins do not load when using the CreateObject command in Excel

Good idea, but it didn't work.

I'm working on this concept at the moment: Extending Word Documents and Excel Workbooks in VSTO Add-ins at Run Time
 
Just curious - why not dig this out of the VBS gutter and into something a little more.. substantial... like VB.NET or c#?
 
Just curious - why not dig this out of the VBS gutter and into something a little more.. substantial... like VB.NET or c#?

That is out of my skillset and I just need to get er' done.

I got it working last night with the help of an engineer in India..
 
Care to share what resolved the issue? Having a very similar issue and it is driving me nuts!
 
Back
Top