Cannot save excel file when started via vbscript/task scheduler..

MedievalDragon

New Member
Joined
Jan 21, 2010
Messages
4
All,

I am having a strange issue. I can run my vbscript from the command prompt or by double clicking on it and all is well. The script starts a hidden excel instance and updates the data and saves the file or creates if not there.
The problem is when I start the script via task scheduler. I see the excel instance start and process data if the file does not exist but it does not save the new file. If the file exists, excel starts but does not look like it is opening the file to process any data. below is my test vbs file...

Any help would be greatly appreciated.

Excel 2007 and 2003 on server 2008

Code:
Option Explicit
'==============================================================='
'==============================================================='
 
'=========================='
' Define constants         '
'=========================='
Dim oFileSystem
Dim oTextFile
Dim oGrantUsersTextFile
Dim oDenyUsersTextFile
Dim oExcelApplication
Dim oExcelWorkbook
Dim oExcelSheet
                
Set oFileSystem = CreateObject("Scripting.FileSystemObject") 
Dim sCurrentDirectory 
''Get and save the current working directory
sCurrentDirectory = CreateObject("Scripting.FileSystemObject").GetFolder(".").Path
wscript.echo "dbg1: " & sCurrentDirectory
Dim sExcelFilePath 
sExcelFilePath = sCurrentDirectory & "\testfile.xlsx"
'' Create excel application
Set oExcelApplication = CreateObject("Excel.Application")
oExcelApplication.Visible = True
oExcelApplication.DisplayAlerts = False
oExcelApplication.AlertBeforeOverwriting = False

If oFileSystem.FileExists(sExcelFilePath) Then
 wscript.echo "db2 (fileexist-true): " &  sCurrentDirectory & "\testfile.xlsx"
 Set oExcelWorkbook = oExcelApplication.Workbooks.Open(sExcelFilePath)
 Set oExcelSheet = oExcelWorkbook.Sheets(1)
 oExcelSheet.Cells(1,1) = Now
 oExcelWorkbook.Save
else        
 wscript.echo "db2 (fileexist-false): " &  sCurrentDirectory & "\testfile.xlsx"
 ' Add workbook
 Set oExcelWorkbook = oExcelApplication.Workbooks.Add
 ' Select sheet1 and rename
 Set oExcelSheet = oExcelWorkbook.Sheets(1)
 oExcelSheet.Name = "GRANT Users"
  oExcelWorkbook.SaveAs(sExcelFilePath)
End If
 

' Save & Close excel application
oExcelWorkbook.Close
oExcelApplication.Quit
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I would suspect it's a permissions issue. What account is the task scheduler running under and where are you trying to save to?
 
Upvote 0
It is running under my Domain Admin account. The file is local to the server. I have tried multiple locations for the file.

It is just weird that it runs manually but not scheduled..

Thanks...
 
Upvote 0
Has a solution been suggested for this? I have a similar situation where I have a .bat file that is successfully scheduled on a Win2003 box and creates the xls. When I schedule it on 2008 (with highest privileges) the script runs but the .xls is not saved. If I log onto the box as the scheduled task user and run it then it works (as it is running in the foreground)
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top