Introduction
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 _
<ClassInterface(ClassInterfaceType.AutoDual)> _
ClassInterfaceType
is available in the System.Runtime.InteropServices
namespace. All we have to do is import that namespace. For example:
<ClassInterface(ClassInterfaceType.AutoDual)> _
Public class YourClass
End class
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, MergeDoc
.
Properties:
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.
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core
Imports System.io
<ClassInterface(ClassInterfaceType.AutoDual)> _
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
Call display(sTempFile)
End Set
End Property
Private Sub display(ByVal sTempFile As String)
sMergeFiles(nCol) = sTempFile
nCol = nCol + 1
ReDim Preserve sMergeFiles(nCol)
End Sub
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
End Set
End Property
Private sOutputFilePath As String
Public WriteOnly Property OutputFilePath() As String
Set(ByVal value As String)
sOutputFilePath = value
End Set
End Property
Public Sub MergeDoc()
Dim missing As Object = System.Type.Missing
Dim pageBreak As Object =
Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak
Dim nRow As Integer
Dim oWord As New Microsoft.Office.Interop.Word.Application
Dim oDoc As Microsoft.Office.Interop.Word.Document =
oWord.Documents.Add()
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
oSel.InsertBreak(pageBreak)
End If
Next
oDoc.SaveAs(sOutputFilePath, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing)
oDoc.Close()
oDoc = Nothing
oWord.Quit(missing, missing, missing)
End Sub
End Class
After creating this class, you have to follow the steps given in the theoretical explanation in order to use it with other languages.
History
- 28 June, 2007 -- Original version posted.