In this article I am going to explain how to access a .NET component from a COM client. I will also give you an example of how to merge two different Word documents into one document using a .NET COM object created with VB.NET.
Using the code
You cannot directly access a .NET component from a COM client. This can be achieved by CCW, a COM callable wrapper that acts as a proxy for the .NET object. To create a COM object for COM clients in .NET, we need to construct the application as a "ClassLibrary." In this type of application, only one class will be created. We have to include this line at top of the public class declaration:
Imports System.Runtime.InteropServices _
ClassInterfaceType is available in the
System.Runtime.InteropServices namespace. All we have to do is import that namespace. For example:
Public class YourClass
Include any methods and properties according to your needs. After you are done with your code and before you build, you have to follow the process outlined below.
- Go to menus, project->properties.
- In the properties window, click the "signing" tab and check the "sign the assembly" checkbox.
- In the combobox, click new and give any name and save it.
- In same properties window, click the "compile" tab and check the "register for COM Interop" checkbox.
After building the application is over, you will get <yourclass>.dll and the xx.snk file. The xx.snk file is the one that you have given in the "signing" tab. Now we have finished creating two files: a DLL file and a strong name DLL file. To use these DLL files on your target machine, you have to perform the following procedure.
First, copy these two files into your target machine and create the x.tlb file using the command in the command line:
regasm <yourclass>.dll /tlb:<anyname>.tlb
After creating the TLB file, we have to put this DLL into the GAC folder using the command:
gacutil –i <yourclass>.dll
You can check that the <yourclass>.dll file is available on your machine by going to either C:\windows\assembly or C:\winnt\assembly, provided that Windows is installed in the C drive. After installing the DLL into your GAC, you can use this COM in any programming language. For example, in VB the <anyname>.tlb file will appear in the "COM" tab of the project reference window. You can check that and use it in your application. A COM created in .NET will refer only to a TLB (Type Library) file, whereas one in VB refers to a DLL file.
Based on the above theoretical explanation, we will now see an example of merging two different Word documents into one document using a .NET COM object created in VB.NET. Before you start, you must decide the version of Word you are using. This is because we have to use Interoperability services in order to make .NET work with Office. For Interoperability services, Microsoft provides different PIAs (Primary Interop Assemblies) for different Office versions. My example here has Office 2003 working with .NET 2005.
Before starting my example, you have to download the PIA for Office 2003 from the Microsoft website and then install and register the PIA on your system. The registration of a particular PIA is given on the Microsoft website itself. After installing the PIA, you can check for it in C:\windows\assembly. Microsoft.Interop.Office.Word.dll should now be available in your system, provided that Windows is installed on the C drive. In this example, I am having one class library that contains three properties and one method,
FilesToMerge: This property is used to assign the names of the documents to be merged.
InsertPageBreaks: When merging different Word documents, if you want any page breaks in between them you will need to set this property to "True." The default is "False."
OutputFilePath: The value of this property is the path for the file where you want to save the merged document.
These are write-only properties, i.e. you can set the values, but you can't get the values. After setting these properties, you should call the
MergeDoc method. The result will be the merged Word document.
Public Class MergeWord
Dim sMergeFiles(0) As String Array containing the file name
with the path to merge.
After accepting as string as file path, it will be stored
in an inner arrays.
Private nCol As Integer = 0
Private sTempFile As String
Public WriteOnly Property FilesToMerge() As String
Set(ByVal value As String)
sTempFile = value
Private Sub display(ByVal sTempFile As String)
sMergeFiles(nCol) = sTempFile
nCol = nCol + 1
ReDim Preserve sMergeFiles(nCol)
If this is true pagebreak will appear otherwise not.
Private bInsertPageBreak As Boolean = True
Public WriteOnly Property InsertPageBreaks() As Boolean
Set(ByVal value As Boolean)
bInsertPageBreak = value
Private sOutputFilePath As String
Public WriteOnly Property OutputFilePath() As String
Set(ByVal value As String)
sOutputFilePath = value
Public Sub MergeDoc()
Dim missing As Object = System.Type.Missing
Dim pageBreak As Object =
Dim nRow As Integer
Dim oWord As New Microsoft.Office.Interop.Word.Application
Dim oDoc As Microsoft.Office.Interop.Word.Document =
Dim oSel As Microsoft.Office.Interop.Word.Selection
oSel = oWord.Application.Selection
Dim nRow As Integer
Dim sFiles() As String
sFiles = sMergeFiles
For nRow = 0 To sFiles.Length - 2
oSel.InsertFile(sFiles(nRow), missing, missing, missing, missing)
If bInsertPageBreak = True Then
oDoc.SaveAs(sOutputFilePath, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing)
oDoc = Nothing
oWord.Quit(missing, missing, missing)
After creating this class, you have to follow the steps given in the theoretical explanation in order to use it with other languages.
- 28 June, 2007 -- Original version posted.