Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.
Posted

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

e-mail: shamil-usersATmns.ru
Posted

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

e-mail: shamil-usersATmns.ru
Posted

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...

e-mail: shamil-usersATmns.ru
Posted

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

e-mail: shamil-usersATmns.ru
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...