[/QUOTE]
After setting a reference to the Excel Object library and declaring 3 string
variables, the code can't be compiled when Option Strict is used. You may
try to use Option Strict because it forces you to do explicit type casting.
This might reveal some errors.
....later...
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oQryTable As Excel.QueryTable
oExcel = DirectCast( _
CreateObject("Excel.Application"), Excel.Application _
)
oBook = oExcel.Workbooks.Add
oSheet = DirectCast(oBook.Worksheets(1), Excel.Worksheet)
'Create the QueryTable object.
oQryTable = oSheet.QueryTables.Add( _
constring, oSheet.Range("A1"), sqlstring _
)
oQryTable.RefreshStyle = Excel.XlCellInsertionMode.xlInsertEntireRows
oQryTable.Refresh(False)
'Save the workbook and quit Excel.
If Dir(fileName) <> "" Then Kill(fileName)
oBook.SaveAs(filename)
oQryTable = Nothing
oSheet = Nothing
oBook.Close()
oBook = Nothing
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
oExcel = Nothing
The code can be compiled now. I didn't test it, but do you still have some
problems?