Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.dotnet.framework.interop,microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel Workbook opened twice with dotnet interop

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(m Client)
Return
End Sub

On the Excel side I have a workbook with a code module that contains:

Private m_App As Object

Public Sub Auto_Open()
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
Auto_Open, 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Immediate Fail: Launch Excel via Interop Doug Price Excel Discussion (Misc queries) 0 January 22nd 10 04:34 PM
Sort excel when workbook opened LTOSH Excel Discussion (Misc queries) 2 October 9th 09 03:40 AM
sort excel when the workbook is opened Michael Excel Discussion (Misc queries) 7 February 1st 07 04:33 PM
OWC load add-ins programmatically in dotnet? ESmith Excel Discussion (Misc queries) 0 September 16th 05 07:36 PM
Can a workbook be opened w/out Excel on the host machine?? MIchelleDuquette Excel Discussion (Misc queries) 2 May 5th 05 08:18 PM


All times are GMT +1. The time now is 03:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"