Generics and Com Visible .NET libraries

后端 未结 1 1813
星月不相逢
星月不相逢 2020-12-20 08:22

I\'ve developed a VB.NET library (partially developed on C# as well) that heavily depends on inheriting from an abstract generic base class, and I\'m trying to figure out th

相关标签:
1条回答
  • 2020-12-20 09:06

    As i mentioned in comment writing library (dll) for MS Office applications is bit... hard-coded. This dll must exposes methods and properties to be able to use it in COM automation. To be able to achieve that, you need to write interface(s):

    Namespace VBtoVBA
        Public Interface IMyDerivedClass
            Property MyProperty As String
        End Interface
    End Namespace
    

    then in DerivedClass

    Public Class MyDerivedClass
        Inherits MyBaseClass(Of String)
        Implements IMyDerivedClass
    
        Private _myProperty As String
    
        Public Property MyProperty As String Implements IMyDerivedClass.MyProperty
    

    Now, go to Project Properties window 1) choose Application tab - click on Assembly Information button and in the next window select Make assembly COM visible checkbox (apply setting using OK button),

    Assembly information

    2) choose Compile tab - select Register for COM interop checkbox

    Register for COM

    3) Save project and Build dll

    4) Now, go to VBA Code editor in Office application -> References menu. In Reference window add reference to yourDllName.tlb

    Now, you can use your dll in Office application ;)

    I tested code:

    Option Explicit
    
    Sub DoSomething()
    Dim m As VBtoVBA.MyDerivedClass
    
    Set m = New VBtoVBA.MyDerivedClass
    
    m.MyProperty = "Something"
    
    MsgBox m.MyProperty
    
    
    End Sub
    

    and it works as well ;)

    0 讨论(0)
提交回复
热议问题