Click here to Skip to main content
Click here to Skip to main content

.NET component from a COM client

, 28 Jun 2007
Rate this:
Please Sign up or sign in to vote.
This article discusses how to access a .NET component from a COM client, with an example of how to merge two different word documents into one

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.

  1. Go to menus, project->properties.
  2. In the properties window, click the "signing" tab and check the "sign the assembly" checkbox.
  3. In the combobox, click new and give any name and save it.
  4. 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.

    'This property accepts only file name with path as string. 
    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


    'Property is used to insert a pagebreak in between each file. 
    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

    'using this property you will give the Path to save the merged file
    Private sOutputFilePath As String
    Public WriteOnly Property OutputFilePath() As String
        Set(ByVal value As String)
            sOutputFilePath = value
        End Set
    End Property

    Public Sub MergeDoc()
    ' Create an object for missing type and pagebreak.    
        Dim missing As Object = System.Type.Missing
        Dim pageBreak As Object = 
            Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak
        Dim nRow As Integer

        ' Create  a new Word application
        Dim oWord As New Microsoft.Office.Interop.Word.Application

        '// Create a new file. you can create based on any template.
        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

        ' Go through the elements in an array one by one and 
        ' insert the file in to a new file.
        For nRow = 0 To sFiles.Length - 2
            oSel.InsertFile(sFiles(nRow), missing, missing, missing, missing)
            ' page break is true then you insert page break between the 
            ' documents.
            If bInsertPageBreak = True Then
                oSel.InsertBreak(pageBreak)
            End If
        Next

        ' After merging the files into a new document, save the 
        ' file into your path,
        ' where your specified in the outputproperty.
        oDoc.SaveAs(sOutputFilePath, missing, missing, missing, 
            missing, missing, missing, missing, missing, missing, missing,
            missing, missing, missing, missing, missing)

        ' Finally close and release the object from memory.
        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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

ponnurajs
Web Developer
India India
No Biography provided

Comments and Discussions

 
GeneralGAC not required... PinmemberHein Bloed27-Jul-07 22:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 28 Jun 2007
Article Copyright 2007 by ponnurajs
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid