Workbook not closed when OnBeginShutdown and OnDisconnection events fire in Excel Addin



I have built a Shared Add-in project using Visual Studio 2003, VB.NET and
Framework 1.1.
It is designed to run in Excel. I modifed the wizard generated code to use
the Excel COM object model.
I added a COM reference to the Excel 11.0 object library. I then declared a
class instance-level variable of type Excel.Application, WithEvents. I then
printed out the Workbooks.Count in the OnBeginShutdown and OnDisconnection
events; and they both show a count of 1.

My goal is to check in the Excel file to a document management system when
the workbook closes, but the file is still in use and the DMS can't move it
from the local system.

Here is my code:

Imports Microsoft.Office.Core
Imports Extensibility
Imports System.Runtime.InteropServices
Imports MOI = Microsoft.Office.Interop

ProgIdAttribute("TestExcelAddin.Connect")> _
Public Class Connect

Implements Extensibility.IDTExtensibility2

Dim WithEvents applicationObject As MOI.Excel.Application
Dim addInInstance As Object

Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
System.Diagnostics.Debug.WriteLine("OnBeginShutdown: WorkBooks.Count
= " & applicationObject.Workbooks.Count)
End Sub

Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
End Sub

Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
End Sub

Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements
System.Diagnostics.Debug.WriteLine("OnDisconnection: WorkBooks.Count
= " & applicationObject.Workbooks.Count)
End Sub

Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = application
addInInstance = addInInst
End Sub
End Class




Hey Alan,
I suppose, by the time the workbook close is called the addin execution
finishes. hence, it looks like a requirement that cant b fullfilled.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question