From: BWCoaster on
Hi,

I have written a vbscript that opens an Excel object and then a workbook,
reads some data and writes it elsewhere. This all works fine.

But in an Excel 2007 SP2 environment, Excel crashes on the .quit (Error is
"...Excel has encountered a problem and needs to close...") I have not been
able to replicate the error with Excel 2003, or Excel 2007 with no SP.

Note that if I set Excel app to visible, the error does not seem to happen -
but visually this is not acceptable - to have Excel flashing up and then
closing, and also is slower to have visible.

This is the script:
'This script should open Excel, open the specified file, read the data and
then write it to a txt file.
'Before running this script, need "C:\Listsa.xlsx" to exist
Option EXPLICIT
Dim oXLApp
Dim oXLBook
Dim MyListFile
dim MyTxtFile
Dim x
Dim fso
dim f
MyListFile="C:\Listsa.xlsx"
MyTxtFile="C:\Liststest.txt"
'Open Excel session
Set oXLApp = CreateObject("Excel.Application")
'can leave invisible to run faster. (set to true to see what is happening
while troubleshooting)
oXLApp.Visible = false
'oXLApp.Visible = true
'open the list file (no conversions, readonly)
Set oXLBook = oXLApp.Workbooks.Open(MyListFile, false, true)
set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(MyTxtFile, 8,true)
f.Writeline "This is a test"
for x = 1 to 4
f.Writeline "A" & x & ":" & oXLBook.worksheets(1).range("A" & x).value
f.Writeline "B" & x & ":" & oXLBook.worksheets(1).range("B" & x).value
next
f.Close
set f = Nothing
set fso = Nothing
'close xls and xl
oXLBook.Close
oXLApp.Quit
Set oXLBook = Nothing
Set oXLApp = Nothing
msgbox "Complete"

Appreciate any thoughts,

Thanks,

B