Excel 12.0 Object Library - EXCEL.EXE Process

PapaDrom
04-29-2010, 02:56 PM
this must have come up a billion times... however after searching for a few hours, I just can't do it anymore and I figured I would ask the real experts.

I striped the code down until it ran without closing excel properly, and it is a Workbooks.add that seems to hold it open but no matter what I do to close it - it doesn't work... I am using rhapsody VBA as the platform currently and have Excel 2007 installed on my machine - late binding - and it is a part of class.. I'll only post what I think is useful / necessary.. in fact I commented out every other line of code in the module and class.

Module

Option Explicit
Public Sub Data_To_Excel()
Dim clsExcel As mde_Office_Excel 'Excel Class
Set clsExcel = New mde_Office_Excel
clsExcel.Create_Workbook
Set clsExcel = Nothing
End Sub


Class

Option Explicit
Dim m_XCEL As Object
Dim m_XCEL_Workbook As Object
Dim m_bXCEL_Flag As Boolean

Private Sub Class_Initialize()
Set m_XCEL = CreateObject("Excel.Application")
m_bXCEL_Flag = True
m_XCEL.Visible = False
End Sub

Private Sub Class_Terminate()
Dim wb As Object
Dim ws As Object
If m_bXCEL_Flag Then
For Each wb In m_XCEL.Workbooks
For Each ws In wb.Worksheets
Set ws = Nothing
Next
wb.Close (False)
Set wb = Nothing
Next
m_XCEL.Workbooks.Close
m_XCEL.DisplayAlerts = False
m_XCEL.quit
Set m_XCEL = Nothing
End If
End Sub

'Creates a new workbook
Public Sub Create_Workbook()
m_XCEL.SheetsInNewWorkbook = 1
Set m_XCEL_Workbook = m_XCEL.Workbooks.Add 'CAUSES HANGING
End Sub


Excel closes fine if this line is commented out
Set m_XCEL_Workbook = m_XCEL.Workbooks.Add 'CAUSES HANGING
or if the entire call to the create workbook method is eliminated from the module. I know the problem LOC just can't fix it.

I need to add a workbook - the terminate method does more and more to ensure this workbook is closed but EXCEL just won't give in... anything I can do to make the OS listen to me besides killing the process... and if thats the true recommended way to do it... then I guess I don't have a choice.

thanks for even looking at this problem -> sorry if this was already answered somewhere but just looking at my terminate statement you can see that I was trying suggestions on a few different google hits.

~Brian

Flyguy
04-30-2010, 03:08 AM
I have had these kind of problems in the past too, also with other nested objects.
I decided not to rely on the Class_Terminate event of an class and always add an Public sub Terminate myself in cases were classes contains references to other objects.

Public Sub Terminate()
If Not m_XCEL_Workbook Is Nothing Then
' ...
End IF

If Not m_XCEL Is Nothing Then
....
End If
End Sub

Private Sub Class_Terminate()
Terminate
End Sub



Option Explicit
Public Sub Data_To_Excel()
Dim clsExcel As mde_Office_Excel 'Excel Class
Set clsExcel = New mde_Office_Excel
clsExcel.Create_Workbook
clsExcel.Terminate
Set clsExcel = Nothing
End Sub

PapaDrom
04-30-2010, 07:37 AM
I guess that would help especally if the terminate method wasn't firing on close - but at least walking through the code it fires and still doesn't clean up excel.

I think I may create a terminate sub routine as you suggest anyways. It feels cleaner to me.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum