Angelus Posted November 13, 2003 Posted November 13, 2003 How to include VB classs in Microsoft Word? I have a class that written in VB.Net. Can it be use in macro in Word (2000)? I try to reference it but I get error message. Quote
*Experts* Volte Posted November 13, 2003 *Experts* Posted November 13, 2003 Not in Word 2000, no. Possibly in 2003, but I haven't looked at it yet. Quote
Angelus Posted November 13, 2003 Author Posted November 13, 2003 What if that class is written in VB 6 ? Can it be used by Word 2000 Quote
Shamil Posted November 13, 2003 Posted November 13, 2003 VB6: You have to put your class into ActiveX Dll and set its Instancing property to MultiUse or GlobalMultiuse. Then you compile your ActiveX Dll and set a reference to it or just use CreateObject(...) to create instances of your class... HTH, Shamil Quote e-mail: shamil-usersATmns.ru
Shamil Posted November 13, 2003 Posted November 13, 2003 VB.NET: Yes, VB.NET managed classes can be called from COM (like MS Word 2000) applications - COM Callable Wrappers (CCW) are used for that - here is an article describing this technology: http://www.codeproject.com/dotnet/COM_DOTNET_INTEROP.asp Here is a sample code: http://www.dotnetextreme.com/articles/ccwrcw.asp Here is a discussion of the CCW subject with some more information how to build visible to COM apps .NET components... HTH, Shamil Quote e-mail: shamil-usersATmns.ru
Shamil Posted November 14, 2003 Posted November 14, 2003 Here is a solution based on info from MSDN and other sources: Test class: ======== Imports System.Runtime.InteropServices <ComClass(TestClass1.ClassId, TestClass1.InterfaceId, TestClass1.EventsId)> _ Public Class TestClass1 #Region "COM GUIDs" Public Const ClassId As String = "FA16F4E8-69AC-4fd1-84BA-770674DFFFAE" Public Const InterfaceId As String = "A041238E-7C8A-437d-A18B-3A18D09E7B60" Public Const EventsId As String = "691E110D-C2FD-4eda-8801-A5246169A4F3" #End Region Public Sub New() MyBase.New() End Sub <DispId(1)> Public Sub MessageBox(ByVal vstrMsg As String) MsgBox("Message from VB.Net component - " & vstrMsg) End Sub <DispId(2)> Public Function GiveMeYourName() As String GiveMeYourName = MyName() End Function <ComVisible(False), DispId(3)> Public Function MyName() As String MyName = Me.GetType().FullName End Function <ComRegisterFunction()> Public Shared Sub OnRegistration(ByVal T As Type) MsgBox(T.FullName & " is being registered in COM!") End Sub End Class AssemblyInfo.VB ============ Imports System.Reflection Imports System.Runtime.InteropServices <Assembly: AssemblyTitle("CCW Test")> <Assembly: AssemblyDescription("Call VB.NET Component from COM")> <Assembly: AssemblyCompany("Test")> <Assembly: AssemblyProduct("CCW Test DLL")> <Assembly: AssemblyCopyright("Public Domain")> <Assembly: AssemblyTrademark("")> <Assembly: CLSCompliant(True)> <Assembly: Guid("DB955828-2239-4747-A08C-5C1F9F574AFA")> <Assembly: AssemblyVersion("1.0.*")> <Assembly: ComVisible(True)> '<Assembly: ClassInterface(ClassInterfaceType.AutoDual)> <Assembly: AssemblyKeyFileAttribute("<type you full path to snk file here>.snk")> Create Assembly Strong Name .snk file =========================== sn -k myKey.snk Create class library and build solution from VS.NET IDE ====================================== (use of VS.NET IDE is obvious) Register assembly and export type library ============================== regasm MyCCWDll.dll /tlb:MyCCWDll.tlb Install Assembly in the global Assembly cashe ================================ gacutil /if MyCCWDll.dll Now you can set reference from COM applications to MyCCWDll.tlb and use its exposed objects via early binding or you can use CreateObject(...) to create instances of the objects exposed from MyCCWDll.dll and use them with late binding... Maybe this above isn't an optimal way to make CCW Dll but it worked for me... HTH, Shamil P.S. The procedure to make CCW components visible to COM should work automagically in theory fro VS.NET IDE if you set "Register for COM interop" checkbox in project's configuration settings but it failed for me... Quote e-mail: shamil-usersATmns.ru
Shamil Posted November 14, 2003 Posted November 14, 2003 Here is even more tricky solution with two implemeneted Interfaces and full control on Interface methods DispIds: Imports System.Runtime.InteropServices <Guid("D9870F22-4CA5-4567-96DA-6A6CAA541F6D")> _ Public Interface ICAllMeFromCOM1 <DispId(1)> Sub MessageBox(ByVal vstrMsg As String) <DispId(2)> Function GiveMeYourName() As String <DispId(3)> Function GetMyRef() As Object End Interface <Guid("780B6E92-AF98-48b6-8C0B-2832C86F2DB7")> _ Public Interface ICallMeFromCOM2 <DispId(503)> Sub testMessage(ByVal vstrMsg As String) End Interface <Guid("AE2F4135-D660-453a-B638-373691194EB6")> _ Public Class TestClass Implements ICAllMeFromCOM1 Implements ICallMeFromCOM2 Public Sub New() MyBase.New() End Sub <DispId(1)> Public Sub MessageBox(ByVal vstrMsg As String) _ Implements ICAllMeFromCOM1.MessageBox MsgBox("Test Message = " + vstrMsg) End Sub <DispId(2)> Public Function GiveMeYourName() As String _ Implements ICAllMeFromCOM1.GiveMeYourName GiveMeYourName = MyName() End Function <DispId(3)> Public Function GetMyref() As Object _ Implements ICAllMeFromCOM1.GetMyRef GetMyref = Me End Function <DispId(503)> Public Sub myMessage(ByVal vstrMsg As String) _ Implements ICallMeFromCOM2.testMessage MsgBox("ICallMeFromCOM2.testMessage: " + vstrMsg) End Sub <ComVisible(False)> Public Function MyName() As String MyName = Me.GetType().FullName End Function <ComRegisterFunction()> Public Shared Sub OnRegistration(ByVal T As Type) MsgBox(T.FullName & " is being registered in COM!") End Sub End Class In Vb6 (or any other COM application) this class above can be used e.g. like this: Dim obj As New TestClass Dim I2 As ICallMeFromCOM2 obj.MessageBox "Test" Set I2 = obj I2.testMessage "Test" Shamil Quote e-mail: shamil-usersATmns.ru
Angelus Posted November 14, 2003 Author Posted November 14, 2003 Thank you everybody. I think that will help me. Quote
Recommended Posts