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
'==============================================================='
'==============================================================='
On Error Resume Next

'=========================='
' Define constants         '
'=========================='
' OpenTextFile Method needs a Const value
' ForAppending = 8 
' ForReading = 1
' ForWriting = 2
Const FOR_READING = 1
Const FOR_APPENDING = 8
const FOR_WRITING = 2
'================================================='
                ' Read email text file
Dim oFileSystem
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 now & " dbg1: " & sCurrentDirectory
Dim sExcelFilePath 
sExcelFilePath = sCurrentDirectory & "\SystemAccess.xlsx"
'' Create excel application
Set oExcelApplication = CreateObject("Excel.Application")
oExcelApplication.Visible = False
oExcelApplication.DisplayAlerts = False
oExcelApplication.Interactive = False
oExcelApplication.AlertBeforeOverwriting = False
oExcelApplication.FeatureInstall = msoFeatureInstallNone
oExcelApplication.DefaultFilePath = "F:\"
If oFileSystem.FileExists(sExcelFilePath) Then
 wscript.echo now & " db2 (fileexist-true): " &  sExcelFilePath
 oExcelApplication.Workbooks.Open sExcelFilePath
' Set oExcelWorkbook = oExcelApplication.Workbooks.Open(sExcelFilePath, false, false)
' Set oExcelSheet = oExcelWorkbook.Sheets(1)
 oExcelApplication.Cells(1,1) = Now
 wscript.echo now & " File saved: " & oExcelApplication.ActiveWorkbook.Saved
 oExcelApplication.Save
 wscript.echo now & " File saved: " & oExcelApplication.ActiveWorkbook.Saved
 do while oExcelApplication.ActiveWorkbook.Saved <> True 
  'oExcelApplication.ActiveWorkbook.Save
  'WScript.Sleep 5000
  oExcelApplication.Save
  oExcelApplication.ActiveWorkbook.Saved = True
 loop
 'For Each w In oExcelApplication.Workbooks
 ' w.Save
 'Next w
else        
 wscript.echo now & "db2 (fileexist-false): " &  sExcelFilePath
 ' Add workbook
 Set oExcelWorkbook = oExcelApplication.Workbooks.Add
 ' Select sheet1 and rename
 Set oExcelSheet = oExcelWorkbook.Sheets(1)
 oExcelSheet.Name = "GRANT Users"
  oExcelWorkbook.SaveAs(sExcelFilePath)
  WScript.Sleep 5000
 wscript.echo now & "File saved: " & oExcelApplication.ActiveWorkbook.Saved
  if oExcelApplication.ActiveWorkbook.Saved <> True then
 
 oExcelWorkbook.SaveAs(sExcelFilePath)
 End if
End If
 

' Save & Close excel application
oExcelApplication.ActiveWorkbook.Close
WScript.Sleep 5000
wscript.echo now & "Workbook Count: " & oExcelApplication.Workbooks.Count
if oExcelApplication.Workbooks.Count = 0 Then
 oExcelApplication.Quit
else
 oExcelWorkbook.Close
 oExcelApplication.Quit
End If
set oExcelApplication = Nothing
Set oFileSystem = Nothing
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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