none
Expose VSTO addin to VBA at design time RRS feed

  • Question

  • I read this post and this article, followed all the steps (including COM registration on Visual Studio side which required to manually regasm Microsoft.Office.Tools.Common.v4.0.Utilities) but I still cannot see my addin on VBA side in Object Browser.  What am I missing?


    Note: My addin is for Word 2010
    • Edited by Phaed0 Saturday, July 7, 2012 10:16 AM
    Saturday, July 7, 2012 10:15 AM

Answers

  • Hi Seb

    OK, I've got it working - more or less - thanks to a tip from someone who doesn't want to be named.

    The problem is that VBA doesn't understand a .NET DLL. You can use regasm.exe (as an administrator!) to create a TLB from the .NET DLL:

    C:\Program Files\Microsoft Visual Studio 9.0\VC>regasm C:\Test\InteropTests\Word
    Addin_VB\WordAddIn_VB.dll /tlb:WordAddIn_VB.tlb

    VBA's Tools/Add references works with this; I can declare an object to the COM_Visible stuff and get Intellisense. Things got tricky when I tried to actually use the code and I discovered the Add-in must be loaded, and I must set the object referencing the TLB to the Add-in object. Sample code:

    Sub CountTaskPanesInAddin()
        Dim VB_Addin As Office.COMAddIn
        Dim addinObject As Object
        
        Set VB_Addin = Application.COMAddIns("WordAddin_VB")
        Set addinObject = VB_Addin.Object
        
        'Set reference to WordAddin_VB.tlb
        'Provides Intellisense
        Dim xAddin As WordAddIn_VB.WordComVisible
        Dim wn As word.Window
        Set wn = ActiveDocument.ActiveWindow
        Set xAddin = addinObject
        xAddin.showMyCtp wn
        
        Debug.Print addinObject.CountCtp ', xAddin.CountCtp 'Compile error: Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.
    End Sub

    But just because we can get Intellisense, it doesn't mean everything actually works! My method showMyCtp, which displays a custom task pane in the specified document window, works fine. But CountCtp, which counts the VSTO custom task panes, does not. This seems logical to me, as the tlb is not running within VSTO, and the objects being counted are purely VSTO objects.

    In summary: it can work, but your mileage may vary. And it will mean distributing and registering the tlb along with the VSTO add-in.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Phaed0 Friday, July 13, 2012 8:52 PM
    Friday, July 13, 2012 7:44 AM
    Moderator

All replies

  • Hi Seb

    If this is a VSTO Add-in, then you should use the steps in the MSDN article. 

    No where does it tell you to manually use regasm. If you're trying to install this on a machine other than the develolpment machine then you need to correctly publish the VSTO add-in, by either creating a ClickOnce or a MSI setup.exe.


    Cindy Meister, VSTO/Word MVP

    Monday, July 9, 2012 7:38 AM
    Moderator
  • Hi Cindy

    I followed the article first, my problem is that on VBA side the AddIn classes/methods/types are not visible which makes programming a lot more difficult.  I do not have this problem with a COM dll.  Andrew Whitechapel's post seemed to provide a solution but maybe it doesn't work with Office/VS 2010?  Is there an updated version of this post?

    Cheers

    s.

    Monday, July 9, 2012 10:54 AM
  • HI Seb

    Ah, OK, I understand what you're asking, now.

    Getting early-binding, so that you'd have Intellisense isn't something I've ever tried with a VSTO add-in. According to Andrew's article, what you'd need to is:

    Open the VBA project in Excel's VBA editor. Go to Tools/References, click Browse, navigate to your installed DLL and select it. It should now appear in the list of activated references for your VBA project and be able to provide Intellisense.

    I don't have an installed VSTO add-in on this machine, so I'm not able to test this for you...


    Cindy Meister, VSTO/Word MVP

    Tuesday, July 10, 2012 4:07 PM
    Moderator
  • Hi Cindy

    I tried to do that and I got an error "Can't add a reference to the specified file" :(

    Cheers

    S.

    Wednesday, July 11, 2012 7:07 PM
  • I tried to do that and I got an error "Can't add a reference to the specified file" :(

    Yes, that's the error I get, as well, when I try to link to a DLL that's only been registered in Debug mode. I was hoping that it might work correctly if the DLL is registered during installation, but apparently the block is elsewhere :-(

    Unfortunately, Andrew Whitechapel moved away from Office quite a while back and as far as I know, he's never written anything additional on the topic. I'm guessing there's some setting required for the DLL to "open it up" at the early-binding level, but I don't know what it is...

    We can try leaving this thread as it stands and hope some one from the "staff" reads through and notices that we're looking for an answer and will do some research for us.


    Cindy Meister, VSTO/Word MVP

    Thursday, July 12, 2012 2:26 PM
    Moderator
  • I haven't done many Excel or Word VSTO addins, only a few, but this certainly works for an Outlook addin as far as calling into a publicly exposed procedure in a VSTO addin. Early binding is non-existent however, and so is Intellisense on the VBA side. Adding a VBA reference to the VATO addin has also never worked for me.
     
    If I have a method PublicFunction() in my addin that's exposed as a pubolic function using the override of the automation servifecs I would get the addin from the COMAddins collection of Outlook (or Word or whatever) as a COMAddin reference. I then use myAddin.Object.PublicFunction() to call the function. I've found in my experience that trying to get myAddin.Object as a separate object and querying that object doesn't seem to work.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Cindy Meister" <=?utf-8?B?Q2luZHkgTWVpc3Rlcg==?=> wrote in message news:b0bb776d-66f8-4e84-ab95-3bc3e2bf3301...

    I tried to do that and I got an error "Can't add a reference to the specified file" :(

    Yes, that's the error I get, as well, when I try to link to a DLL that's only been registered in Debug mode. I was hoping that it might work correctly if the DLL is registered during installation, but apparently the block is elsewhere :-(

    Unfortunately, Andrew Whitechapel moved away from Office quite a while back and as far as I know, he's never written anything additional on the topic. I'm guessing there's some setting required for the DLL to "open it up" at the early-binding level, but I don't know what it is...

    We can try leaving this thread as it stands and hope some one from the "staff" reads through and notices that we're looking for an answer and will do some research for us.


    Cindy Meister, VSTO/Word MVP


    Ken Slovak MVP - Outlook
    Thursday, July 12, 2012 2:43 PM
  • Hi Seb

    OK, I've got it working - more or less - thanks to a tip from someone who doesn't want to be named.

    The problem is that VBA doesn't understand a .NET DLL. You can use regasm.exe (as an administrator!) to create a TLB from the .NET DLL:

    C:\Program Files\Microsoft Visual Studio 9.0\VC>regasm C:\Test\InteropTests\Word
    Addin_VB\WordAddIn_VB.dll /tlb:WordAddIn_VB.tlb

    VBA's Tools/Add references works with this; I can declare an object to the COM_Visible stuff and get Intellisense. Things got tricky when I tried to actually use the code and I discovered the Add-in must be loaded, and I must set the object referencing the TLB to the Add-in object. Sample code:

    Sub CountTaskPanesInAddin()
        Dim VB_Addin As Office.COMAddIn
        Dim addinObject As Object
        
        Set VB_Addin = Application.COMAddIns("WordAddin_VB")
        Set addinObject = VB_Addin.Object
        
        'Set reference to WordAddin_VB.tlb
        'Provides Intellisense
        Dim xAddin As WordAddIn_VB.WordComVisible
        Dim wn As word.Window
        Set wn = ActiveDocument.ActiveWindow
        Set xAddin = addinObject
        xAddin.showMyCtp wn
        
        Debug.Print addinObject.CountCtp ', xAddin.CountCtp 'Compile error: Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.
    End Sub

    But just because we can get Intellisense, it doesn't mean everything actually works! My method showMyCtp, which displays a custom task pane in the specified document window, works fine. But CountCtp, which counts the VSTO custom task panes, does not. This seems logical to me, as the tlb is not running within VSTO, and the objects being counted are purely VSTO objects.

    In summary: it can work, but your mileage may vary. And it will mean distributing and registering the tlb along with the VSTO add-in.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Phaed0 Friday, July 13, 2012 8:52 PM
    Friday, July 13, 2012 7:44 AM
    Moderator
  • Hi Cindy
    Thank you very much - I can't test your answer right now but I certainly will in due time
    S.
    Friday, July 13, 2012 8:52 PM