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
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: