65.9K
CodeProject 正在变化。 阅读更多。
Home

来自 COM 客户端的 .NET 组件

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.67/5 (2投票s)

2007 年 6 月 28 日

4分钟阅读

viewsIcon

28326

本文讨论了如何从 COM 客户端访问 .NET 组件,并提供了一个如何将两个不同的 Word 文档合并为一个文档的示例。

引言

在本文中,我将解释如何从 COM 客户端访问 .NET 组件。我还将提供一个示例,说明如何使用 VB.NET 创建的 .NET COM 对象将两个不同的 Word 文档合并为一个文档。

使用代码

您无法直接从 COM 客户端访问 .NET 组件。这可以通过 CCW 实现,CCW 是一个 COM 可调用包装器,充当 .NET 对象的代理。 要在 .NET 中为 COM 客户端创建 COM 对象,我们需要将应用程序构建为“ClassLibrary”。 在这种类型的应用程序中,只会创建一个类。 我们必须在公共类声明的顶部包含这一行

Imports System.Runtime.InteropServices _
    <ClassInterface(ClassInterfaceType.AutoDual)> _ 

ClassInterfaceTypeSystem.Runtime.InteropServices 命名空间中可用。 我们所要做的就是导入该命名空间。 例如

<ClassInterface(ClassInterfaceType.AutoDual)> _
Public class YourClass
End class

根据您的需要包含任何方法和属性。 完成代码并构建之前,您必须按照下面概述的过程操作。

  1. 转到菜单,项目->属性。
  2. 在属性窗口中,单击“签名”选项卡,然后选中“对程序集签名”复选框。
  3. 在组合框中,单击新建并给出一个名称并保存。
  4. 在同一属性窗口中,单击“编译”选项卡,然后选中“注册 COM Interop”复选框。

构建应用程序完成后,您将获得 <yourclass>.dllxx.snk 文件。 xx.snk 文件是您在“签名”选项卡中给出的文件。 现在我们已经完成了创建两个文件:一个 DLL 文件和一个强名称 DLL 文件。 要在目标计算机上使用这些 DLL 文件,您必须执行以下步骤。

首先,将这两个文件复制到您的目标计算机中,并使用命令行中的命令创建 x.tlb 文件

regasm <yourclass>.dll /tlb:<anyname>.tlb

创建 TLB 文件后,我们必须使用以下命令将此 DLL 放入 GAC 文件夹

gacutil –i <yourclass>.dll

您可以检查 <yourclass>.dll 文件是否在您的机器上可用,方法是转到 C:\windows\assembly 或 C:\winnt\assembly,前提是 Windows 安装在 C 盘上。 将 DLL 安装到 GAC 后,您可以在任何编程语言中使用此 COM。 例如,在 VB 中,<anyname>.tlb 文件将出现在项目引用窗口的“COM”选项卡中。 您可以检查并将其用于您的应用程序。 在 .NET 中创建的 COM 将仅引用 TLB(类型库)文件,而在 VB 中创建的 COM 将引用 DLL 文件。

基于上述理论解释,我们现在将看到一个使用在 VB.NET 中创建的 .NET COM 对象将两个不同的 Word 文档合并为一个文档的示例。 在开始之前,您必须确定您正在使用的 Word 版本。 这是因为我们必须使用互操作服务才能使 .NET 与 Office 协同工作。 对于互操作服务,Microsoft 为不同的 Office 版本提供不同的 PIA(主互操作程序集)。 我的示例是 Office 2003 与 .NET 2005 协同工作。

在开始我的示例之前,您必须从 Microsoft 网站下载 Office 2003 的 PIA,然后在您的系统上安装和注册 PIA。 特定 PIA 的注册在 Microsoft 网站本身上给出。 安装 PIA 后,您可以检查 C:\windows\assembly。 Microsoft.Interop.Office.Word.dll 现在应该在您的系统中可用,前提是 Windows 安装在 C 盘上。 在此示例中,我有一个类库,其中包含三个属性和一个方法 MergeDoc

属性

  • FilesToMerge:此属性用于分配要合并的文档的名称。
  • InsertPageBreaks:合并不同的 Word 文档时,如果您想在它们之间插入任何分页符,您将需要将此属性设置为“True”。 默认值为“False”。
  • OutputFilePath:此属性的值是您要保存合并文档的文件的路径。

这些是只写属性,即您可以设置值,但无法获取值。 设置这些属性后,您应该调用 MergeDoc 方法。 结果将是合并的 Word 文档。

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

创建此类后,您必须按照理论解释中给出的步骤,以便将其与其他语言一起使用。

历史

  • 2007 年 6 月 28 日 -- 发布原始版本。
© . All rights reserved.