Excel Automation from COM+ (VB) - "Method ~ of ~ failed"
Posted by Bert Nieves on October 12, 2001 10:15 AM
PLATFORM: Windows 2000 Advanced Server (SP2)
OFFICE: Excel 2000 (SR1)
DEV PLATFORM: Visual Basic 6.0 (SP5) - COM+ component
Greetings everyone,
I'm automating Excel from VB code. I'm just loading a .csv file and performing some formatting and saving the correpsonding .xls file (xlWorkbook Normal Save As) onto the filesystem.
Everything works fine but when my component is complete .. the Excel process stays hanging around and doesn't properly exit. Any subsequent calls to the component returns a "Method ~ of ~ failed" error message.
Why isn't the Excel process cleaning up (exiting). I supplied a small snippet to show my instantiation of Excel and my cleanup. Thanks in advance.
<begin snippet>
Dim oExcel As Excel.Application
Dim oWorkbook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oFormatRange As Excel.Range
Dim strOutputFilename As String
Dim strOutputFileExt As String
Dim strOutputDirectory As String
Dim strOutputPath As String
On Error Resume Next
'Invoke Excel Application
Set oExcel = GetObject(, "Excel.Application")
On Error GoTo ErrorHandler
If Err.Number <> 0 Or (Not oExcel Is Nothing = False) Then
Set oExcel = CreateObject("Excel.Application")
Err.Clear
End If
oExcel.Visible = False
oExcel.DisplayAlerts = False
Set oWorkbook = oExcel.Workbooks.Open(...)
.........................
........................
'Cleanup, cleanup, cleanup
If Not oFormatRange Is Nothing Then Set oFormatRange = Nothing
If Not oSheet Is Nothing Then Set oSheet = Nothing
If Not oWorkbook Is Nothing Then Call oWorkbook.Close(False)
If Not oExcel Is Nothing Then Call oExcel.Quit
If Not oWorkbook Is Nothing Then Set oWorkbook = Nothing
If Not oExcel Is Nothing Then Set oExcel = Nothing
.....
....
<end snippet>