Quantcast
Channel: NetOffice - MS Office in .NET
Viewing all articles
Browse latest Browse all 1741

New Post: Receiving all events twice in Excel COM add in

$
0
0
I am a new user to NetOffice. I am really impressed with what you have done with wrapping COM objects and version independence.

I think I must be adding my event handlers wrong because I am receiving all events twice in my handlers. I have tried two techniques but am getting the same result.

First technique (AddHandler):
Public Class Addin
   Inherits Excel.Tools.COMAddin

    Public Sub New()
        Dim onStartupCompleteHandler As OnStartupCompleteEventHandler = AddressOf Me.Addin_OnStartupComplete
        AddHandler Me.OnStartupComplete, onStartupCompleteHandler
    End Sub

    Private Sub Addin_OnStartupComplete(ByRef custom As System.Array) Handles Me.OnStartupComplete
        Dim application_WorkbookActivateEventHandler As Excel.Application_WorkbookActivateEventHandler = AddressOf Me.Addin_Application_WorkbookActivateEventHandler
        AddHandler Application.WorkbookActivateEvent, application_WorkbookActivateEventHandler
         Debug.WriteLine($"Addin started in Excel Version {Application.Version}")
    End Sub

    Private Sub Addin_Application_WorkbookActivateEventHandler(Wb As Excel.Workbook)
        Debug.WriteLine($"Workbook {Wb.Name} activated.")
    End Sub
End Class
Second technique (WithEvents):
Public Class Addin
   Inherits Excel.Tools.COMAddin

    Dim _Events As ApplicationEvents
    Public Sub New()
        Dim onStartupCompleteHandler As OnStartupCompleteEventHandler = AddressOf Me.Addin_OnStartupComplete
        AddHandler Me.OnStartupComplete, onStartupCompleteHandler
    End Sub

    Private Sub Addin_OnStartupComplete(ByRef custom As System.Array) Handles Me.OnStartupComplete
       _Events = New ApplicationEvents(Application)
        Debug.WriteLine($"Addin started in Excel Version {Application.Version}")
    End Sub
End Class

Public Class ApplicationEvents

    Private WithEvents _Application As Excel.Application

    Public Sub New(application As Excel.Application)
        If application Is Nothing Then Throw New ArgumentNullException(NameOf(application))
        _Application = application
    End Sub

    Private Sub Addin_Application_WorkbookActivateEventHandler(Wb As Excel.Workbook) Handles _Application.WorkbookActivateEvent
        Debug.WriteLine($"Workbook {Wb.Name} activated.")
    End Sub
End Class
This StackOverflow answer http://stackoverflow.com/a/30895082/3175562 got me thinking that I might be adding my handler when it is already linked, thus my handler would be getting called twice, but that doesn't really make sense. Even the onStartupCompleteHandler is getting called twice.

Thanks for any insight you guys can provide.

Viewing all articles
Browse latest Browse all 1741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>