S
Steve Weixel
I have a strange problem where Excel (2000) ends up with the same file
opened up twice, and then of course neither of them work anymore. I was able
to recreate the problem outside of my application by creating a simple
dotnet class that exposes itself to COM. There are two methods in the class,
Initialize and Quit:
Private mClient As Object
Public Function Initialize(ByVal client As Object) As Boolean
mClient = client
Return True
End Function
Public Sub Quit()
Runtime.InteropServices.Marshal.ReleaseComObject(mClient)
Return
End Sub
On the Excel side I have a workbook with a code module that contains:
Private m_App As Object
Public Sub Autpen()
On Error GoTo err_handler
'Create and intialize the app if it isn't already running.
If m_App Is Nothing Then
Set m_App = CreateObject("ComTest.Application")
If Not m_App Is Nothing Then
If m_App.Initialize(ThisWorkbook) Then
MsgBox "initalized"
Else
MsgBox "failed"
End If
End If
End If
'Make sure that there are no residual errors.
Err.Clear
err_handler:
If Not Err.Number = 0 Then
MsgBox Err.Description
End If
End Sub
Public Sub Auto_Close()
On Error Resume Next
'Force the application obhect to close.
If Not m_App Is Nothing Then
m_App.Quit
Set m_App = Nothing
End If
Err.Clear
End Sub
This all works great most of the time. When the Excel workbook is opened, it
starts up the dotnet part and the two sides talk to each other until Excel
is closed, and everything shuts down properly, closing all of the objects,
disconnecting the dotnet part and the excel.exe process is gone from the
task list. Also if I close the workbook, everything shuts down properly and
I can reopen the workbook and start all over if I want to.
Where things start failing, however, is if I try to open the same workbook
again (e.g. by double-clicking on it from explorer). What appears to happen
is that Excel closes the currently open instance of the workbook, and then
opens it again. However, because we called App.Initialize(ThisWorkbook) in
Autpen, there appears to be an RCW-wrapped reference to the workbook that
is preventing it from closing properly. Excel then opens the workbook again
and we have two of the same workbook open. Trying to access many parts of
the workbook cause "Out of memory" errors, and buttons and menus that had
their onactions pointed to the workbook no longer work properly because they
are referencing the new workbook which is not properly initialized.
The kicker in the whole thing is that "Auto_Close" is not being called as it
is when you close the workbook normally, so I don't have a chance to
disconnect the dotnet reference. I also tried adding the appropriate
workbook events (i.e. BeforeClose, Deactivate), but those too are not called
in this situation.
Does anyone have any ideas? I am completely stumped, and am just about ready
to give up and call the problem unfixable.
Thanks in advance,
- Steve
opened up twice, and then of course neither of them work anymore. I was able
to recreate the problem outside of my application by creating a simple
dotnet class that exposes itself to COM. There are two methods in the class,
Initialize and Quit:
Private mClient As Object
Public Function Initialize(ByVal client As Object) As Boolean
mClient = client
Return True
End Function
Public Sub Quit()
Runtime.InteropServices.Marshal.ReleaseComObject(mClient)
Return
End Sub
On the Excel side I have a workbook with a code module that contains:
Private m_App As Object
Public Sub Autpen()
On Error GoTo err_handler
'Create and intialize the app if it isn't already running.
If m_App Is Nothing Then
Set m_App = CreateObject("ComTest.Application")
If Not m_App Is Nothing Then
If m_App.Initialize(ThisWorkbook) Then
MsgBox "initalized"
Else
MsgBox "failed"
End If
End If
End If
'Make sure that there are no residual errors.
Err.Clear
err_handler:
If Not Err.Number = 0 Then
MsgBox Err.Description
End If
End Sub
Public Sub Auto_Close()
On Error Resume Next
'Force the application obhect to close.
If Not m_App Is Nothing Then
m_App.Quit
Set m_App = Nothing
End If
Err.Clear
End Sub
This all works great most of the time. When the Excel workbook is opened, it
starts up the dotnet part and the two sides talk to each other until Excel
is closed, and everything shuts down properly, closing all of the objects,
disconnecting the dotnet part and the excel.exe process is gone from the
task list. Also if I close the workbook, everything shuts down properly and
I can reopen the workbook and start all over if I want to.
Where things start failing, however, is if I try to open the same workbook
again (e.g. by double-clicking on it from explorer). What appears to happen
is that Excel closes the currently open instance of the workbook, and then
opens it again. However, because we called App.Initialize(ThisWorkbook) in
Autpen, there appears to be an RCW-wrapped reference to the workbook that
is preventing it from closing properly. Excel then opens the workbook again
and we have two of the same workbook open. Trying to access many parts of
the workbook cause "Out of memory" errors, and buttons and menus that had
their onactions pointed to the workbook no longer work properly because they
are referencing the new workbook which is not properly initialized.
The kicker in the whole thing is that "Auto_Close" is not being called as it
is when you close the workbook normally, so I don't have a chance to
disconnect the dotnet reference. I also tried adding the appropriate
workbook events (i.e. BeforeClose, Deactivate), but those too are not called
in this situation.
Does anyone have any ideas? I am completely stumped, and am just about ready
to give up and call the problem unfixable.
Thanks in advance,
- Steve