none
subscribe to an event, declared in Word add-in RRS feed

  • Question

  • Hello

    I am developing an add-in to MS Excel, I have declared Macro interface in it, that can be used from VBA code in Word add-in like this:

    Set addIn = Application.COMAddIns("AddinName")   
    
    addIn.Object.MethodCall

    now I want to expose an event to which it is possible to subscribe from VBA. how such code will look in VBA? addin instance type is not declared in VBA and I cannot subscribe to even using common <instance>_<event_name> approach. any ideas?

    this doesn't work:

    ...
    AddHandler addIn.Object.EventName, AddressOf EventHandler1
       
     
    End Sub
    
    Private Sub EventHandler1()
        MsgBox "Event"
    End Sub

    Monday, September 12, 2016 1:33 PM

Answers

  • .Net dlls cannot be added as references to VBA objects since their COM plumbing comes form the .Net run-time.

    You need to add a reference from the list of available COM objects. not by browsing to the dll.  


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by koshdim Wednesday, September 14, 2016 7:40 AM
    Tuesday, September 13, 2016 5:59 PM

All replies

  • You need to add a reference to the type library exposing the interface with its events to your VBA project.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, September 12, 2016 3:48 PM
  • thanks Dmitry,

    when I try to add reference I get

    ---------------------------
    Microsoft Visual Basic for Applications
    ---------------------------
    Can't add a reference to the specified file.
    ---------------------------
    OK   Help   
    ---------------------------

    error dialog. dll is .Net assembly. what is the problem?

    Tuesday, September 13, 2016 8:08 AM
  • .Net dlls cannot be added as references to VBA objects since their COM plumbing comes form the .Net run-time.

    You need to add a reference from the list of available COM objects. not by browsing to the dll.  


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by koshdim Wednesday, September 14, 2016 7:40 AM
    Tuesday, September 13, 2016 5:59 PM
  • ok, I've got it registered

    Microsoft .NET Framework Assembly Registration Utility version 4.6.1055.0
    for Microsoft .NET Framework version 4.6.1055.0
    Copyright (C) Microsoft Corporation.  All rights reserved.

    Types registered successfully

    but I don't see types declared with COMVisible=true attribute in the list. do I need to do something more?

    Wednesday, September 14, 2016 10:58 AM
  • nevermind, I forgot the /tlb key. found the solution here
    Wednesday, September 14, 2016 1:10 PM