Link to home
Start Free TrialLog in
Avatar of killswitch
killswitch

asked on

Running a Execl macro from vbs via scheduled tasks leaves EXCEL.EXE process running and Excel file as read only.

I created a vbs script to open a Excel file which runs a macro and updates the sheets. The vbs script runs fine if you just run it. The Excel file opens the macro runs and the file gets updated and closes. The problem I am encountering is when I run the vbs file from Scheduled Tasks it opens the file, updates the
sheet but it leaves the process EXCEL.EXE running and if you try to open the file it can only open as read only. I have tried putting in a sleep thinking a delay would hlep, and I have looked at user/file permissions. I am using Excel 2000, Access 2000(used in the macro), on a Windows 2003 server.
strFile = "C:\Documents and Settings\srv1admin\My Documents\Sales Reports LonRon\Total pairs by category and brands.xls"
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
	("C:\Documents and Settings\srv1admin\My Documents\Sales Reports LonRon\Total pairs by category and brands.xls")
 
objExcel.Visible = True
objExcel.Run("RunAll08")
 
 
'Test save and close below
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile
 
 
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
 
 
objExcel.DisplayAlerts = False
objExcel.Application.Quit

Open in new window

Avatar of Iced-evil
Iced-evil
Flag of Belgium image

you could try and kill the excel process with te vbs (its not that clean)

an example can be found here:
http://www.computerperformance.co.uk/vbscript/wmi_process_stop.htm
Avatar of killswitch
killswitch

ASKER

Hmmm, I see what you mean. I hesitate to use that approach due to the fact other users may have an excel process running. Currently there is a user with that process so I shall test later. Thanks for your quick response.
Try releasing the object explicitly.  VB should do this automatically, but sometimes the cleanup isn't that good.

One point seems odd though... If the file is still locked, it implies that the Save/Close methods haven't completed successfully - which is why Excel is still open.  As Excel is visible, does it look like it saves and closes it ok?

strFile = "C:\Documents and Settings\srv1admin\My Documents\Sales Reports LonRon\Total pairs by category and brands.xls"
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
        ("C:\Documents and Settings\srv1admin\My Documents\Sales Reports LonRon\Total pairs by category and brands.xls")
 
objExcel.Visible = True
objExcel.Run("RunAll08")
 
 
'Test save and close below
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile
 
 
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
 
 
objExcel.DisplayAlerts = False
objExcel.Application.Quit
set objExcel=Nothing

Open in new window

This situation only occurs if you run it from Scheduled Tasks. The script runs fine if you just run it. I tried the code suggested and I still have the same issue. Thanks for your input.
Try commenting out the alerts =false long enough to see if excel is prompting you for something that is not allowing it to close properly.  Reading that this only occurs running from a S.T. but still worth looking at.

zf
I just set up some Excel macros to run on a schedule (every fifteen minutes), and they work fine.  However, I don't set up the script on the schedule - I call the script from a batch file (it is the batch file that is scheduled).
The batch file simply contains the one line below.  One problem with VBS can be that there are two scripting engines - if wscript is the default, you can encounter problems when automating (there may well be a prompt that you cannot see as it is running in another account).  The below batch file forces cscript as the engine.

cscript script.vbs

Open in new window

All, I apologize for my late response. Update: I work from RDC. I have successfully run the vb script. I have run the script from a batch file successfully. However, I still see the EXCELL.EXE process running when it is run from Scheduled Tasks. So, what is happening is that when running the script (or batch file) from Scheduled Tasks it runs on the console of the server. When you logon to the server console after running it from Scheduled Tasks you can see an error message Microsoft Excel Not enough system resources to display completely.

So, when you are logged on the server console and run the script or batch file from Scheduled Tasks via RDC you can watch it run successfully on the server console. If the console is locked it fails with the above error message. Microsoft Excel Not enough system resources to display completely.

To summarize if I run the script from RDC I can watch it run successfully. If it is run from Scheduled Tasks it runs on the system (server) console and the error Microsoft Excel Not enough system resources to display completely occurs. Here is the caveat: If the user is logged on to the console the script runs successfully from Scheduled Tasks as you can watch it execute successfully.

I would have never found this out except I was in the computer room and I logged onto the console to update our anti-virus software and I saw the excel file open and the above error. Also, of note there are no related errors in the event viewers.

There is a Experts article that addresses this error but, I am not sure if it is relevant in this situation because the script runs correctly under certain circumstances and the article deals mainly with formatting.
https://www.experts-exchange.com/questions/21837567/Excel-not-enough-system-resources-to-display-completely.html

The issues seem to be if a user is logged onto the console or not.
Initially I thought this was a vb script problem now I am not so sure.

Thanks for all your troubleshooting tips so far with this issue.
Regards
What account is the scheduled task running under?
ASKER CERTIFIED SOLUTION
Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Although I thought I had commented that line out during all the different attempts to get this to run that was it. Youre a genius. Now I can sleep at night again, very cool. Thanks.
Sometimes the obvious is overlooked and we learn. Thanks.