如何从 Internet Explorer 中将 VB ActiveX 组件用于 Word...

来源:百度文库 编辑:神马文学网 时间:2024/04/29 05:40:13

如何从 Internet Explorer 中将 VB ActiveX 组件用于 Word 自动化

查看本文应用于的产品

本页

  • 概要
    • 更多信息
      • 下载示例
        • 设置示例
          • Visual Basic ActiveX 组件
            • 从网页使用 ActiveX 组件
          • 参考
            展开全部 | 关闭全部

            概要 本文说明了您如何能够从 Internet Explorer 中呈现的网页中将 ActiveX 组件用于 Word 的客户端自动化。使用网页中的 ActiveX...

            本文说明了您如何能够从 Internet Explorer 中呈现的网页中将 ActiveX 组件用于 Word 的客户端自动化。使用网页中的 ActiveX 组件而不使用嵌入到网页本身的脚本有几个好处:
            • 如果您已经具有使 Microsoft Word 自动化的 Visual Basic 代码,则可以通过将您的 Visual Basic 项目转换为 ActiveX EXE 或 ActiveX DLL 在浏览器中重新使用您的代码。
            • Word 没有标记为可以安全地使用脚本。根据 Internet Explorer 中的安全设置不同,脚本中的“Word 自动化”代码可能没有运行,或者,用户可能会得到安全警告提示。假定您的 ActiveX 组件满足特定规则,它可能会被标记为可安全处理脚本以避免这些安全问题。
            • Visual Basic 有几个不能与网页脚本一起使用的功能。例如,调用 Windows 应用程序编程接口 (API) 这样一项功能,Visual Basic 有,但网页脚本没有。
            常见的开发人员会向用户提供网页接口,以便使用来自外部源或逻辑的数据创建 Word 文档。虽然您可以使用服务器端“Word 自动化”来生成文档,然后将其传送回客户端,但是使用服务器端方法处理“Word 自动化”存在许多缺点。主要缺点是可伸缩性,Word 是占用资源较多的“自动化”服务器,建议不要用它在 Web 服务器上生成文档。

            通过使用 ActiveX 组件在客户端执行文档生成,您可以将要占用大量资源的“Word 自动化”从 Web 服务器上移走。这就是本文中讨论的示例 ActiveX 组件所提供的解决方案。虽然该示例特定于“Word 自动化”,但是同样的原理可以应用于自动化其他 Microsoft Office 应用程序,例如 Microsoft Excel。 回到顶端

            更多信息 下载示例AutoWord.exe 包含本文中介绍的 Visual Basic ActiveX DLL 项目、Word 文档和网页。从 Microsoft 下载中...

            下载示例

            AutoWord.exe 包含本文中介绍的 Visual Basic ActiveX DLL 项目、Word 文档和网页。

            从 Microsoft 下载中心可以下载以下文件:
            Autoword.exe (http://download.microsoft.com/download/word2002/sample/1.0/win98mexp/en-us/autoword.exe) 发布日期:2001 年 4 月 17 日

            有关如何下载 Microsoft 支持文件的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 119591  (http://support.microsoft.com/kb/119591/ ) 如何从联机服务获取 Microsoft 支持文件 Microsoft 已对此文件进行了病毒扫描。Microsoft 使用的是该文件发布时可以获得的最新病毒检测软件。该文件存储在安全性得到增强的服务器上,以防止在未经授权的情况下对其进行更改。 回到顶端

            设置示例

            下载了 Autoword.exe 以后,请按照以下步骤设置该示例:
            1. 在您的 Web 服务器的虚拟根目录中创建一个名为“Invoice”的文件夹。(默认的虚拟根目录是 C:\Inetpub\Wwwroot。)
            2. 将 Autoword.exe 中的文件解压缩到“Invoice”文件夹。
            3. 在文本编辑器或 HTML 编辑器中打开 Autoword1.htm,然后将所有 URL 中对 YourWebServer 的引用替换为您的 Web 服务器名称。同样,将连接字符串中的 YourSQLServer 替换为包含罗斯文示例数据库的 SQL Server 的名称。

              注意:如果您不具有包含罗斯文示例数据库的可用 SQL Server,则可以修改连接字符串,以便改为使用 Microsoft Access 罗斯文示例数据库。到 Access 罗斯文示例数据库的连接类似如下:
              sConn = "provider=microsoft.jet.oledb.4.0; data source=" & _                        "C:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb"                        
            4. 在文本编辑器或 HTML 编辑器中打开 Autoword2.htm,然后将所有 URL 中对 YourWebServer 的引用替换为您的 Web 服务器的名称。
            5. 启动 Internet Explorer。您可以浏览 http://YourWebServer/invoice/AutoWord1.htm 和 http://YourWebServer/invoice/AutoWord2.htm 来测试该脚本。您在首次访问这两个页面中的任何一个时,会看到让您下载 ActiveX 组件的提示。
            下面的几个部分会更详细地介绍示例 Visual Basic ActiveX 组件和脚本。

            Visual Basic ActiveX 组件

            该示例中的 Visual Basic ActiveX 组件与网页脚本进行交互,以生成用户请求的订单发票文档。Web 应用程序可以允许 ActiveX 组件获取某个指定订单 ID 的订单信息,或者,Web 应用程序可以选择将订单信息打包成 XML,然后将其发送给 ActiveX 组件以进行处理。无论是哪种情况,当组件获得了订单信息后,它都会对 Word 进行自动化以生成和显示该订单的发票文档。

            ActiveX 组件 (AutomateWord) 包含一个类,即 Invoice 类,该类公开三个方法:
            • GetData 方法使用“ActiveX 数据对象”(ADO) 来提取罗斯文示例数据库中有关订单的信息。订单信息存储在 m_Data 专有成员变量中。可以调用 GetData 方法以便让数据提取发生在客户端。
            • SendData 方法使用 Microsoft XML (MSXML),用调用方所提供的订单信息来填充 m_Data 专有成员变量。SendData 会要求一个表示订单信息的 DOMDocument 对象的参数。可以调用 SendData 方法,以便将订单信息从网页发送到该组件。使用此方法,您可以使用 ASP 来提取服务器端数据,并向客户端提供可用于生成文档的 XML 数据岛。
            • MakeInvoice 方法使用“Word 自动化”在 m_Data 专有成员变量中生成包含订单信息的文档。一个存储在 Web 服务器上的文档用作该发票的起始点。调用方可以选择在浏览器的外面显示已完成的 Word 文档,也可以将已完成的文档保存到磁盘供以后使用。
            Invoice.cls
            Option Explicit                    Private Type InvoiceData                    OrderID As String                    OrderDate As Date                    CustID As String                    CustInfo As String                    ProdInfo As Variant                    End Type                    Private m_Data As InvoiceData                    Public Sub GetData(sOrderID As Variant, sConn As Variant)                    Dim oConn As Object, oRS As Object                    'Connect to the Northwind database.                    Set oConn = CreateObject("ADODB.Connection")                    oConn.Open sConn                    'Obtain the Customer ID and Order Date.                    Set oRS = CreateObject("ADODB.Recordset")                    oRS.Open "Select [OrderDate], [CustomerID] from Orders where " & _                    "[OrderID]=" & sOrderID, oConn, 3 'adOpenStatic=3                    m_Data.OrderID = sOrderID                    m_Data.OrderDate = CDate(oRS.Fields("OrderDate").Value)                    m_Data.CustID = oRS.Fields("CustomerID").Value                    oRS.Close                    'Obtain Customer information.                    Set oRS = CreateObject("ADODB.Recordset")                    oRS.Open "Select * from Customers Where CustomerID='" & _                    m_Data.CustID & "'", oConn, 3 'adOpenStatic=3                    m_Data.CustInfo = oRS.Fields("CompanyName").Value & vbCrLf & _                    oRS.Fields("City") & " "                    If Not (IsNull(oRS.Fields("Region"))) Then                    m_Data.CustInfo = m_Data.CustInfo & oRS.Fields("Region").Value & " "                    End If                    m_Data.CustInfo = m_Data.CustInfo & oRS.Fields("PostalCode").Value & _                    vbCrLf & oRS.Fields("Country").Value                    oRS.Close                    'Obtain Product information.                    Set oRS = CreateObject("ADODB.Recordset")                    oRS.Open "Select ProductName, Quantity, [Order Details].UnitPrice," & _                    "Discount from Products Inner Join [Order Details] on " & _                    "Products.ProductID = [Order Details].ProductID " & _                    "Where OrderID = " & sOrderID, oConn, 3 'adOpenStatic=3                    m_Data.ProdInfo = oRS.GetRows                    oRS.Close                    'Close the connection to the database.                    oConn.Close                    End Sub                    Public Sub SendData(oXML As Variant)                    'Extract the information from the DOMDocument object oXML and store                    'it in the private member variable m_Data.                    m_Data.OrderID = oXML.getElementsByTagName("OrderID").Item(0).Text                    m_Data.OrderDate = oXML.getElementsByTagName("OrderDate").Item(0).Text                    m_Data.CustID = oXML.getElementsByTagName("CustID").Item(0).Text                    m_Data.CustInfo = oXML.getElementsByTagName("CustInfo").Item(0).Text                    Dim oItems As Object, oItem As Object                    Set oItems = oXML.getElementsByTagName("Items").Item(0)                    ReDim vArray(0 To 3, 0 To oItems.childNodes.Length - 1) As Variant                    Dim i As Integer                    For i = 0 To UBound(vArray, 2)                    Set oItem = oItems.childNodes(i)                    vArray(0, i) = oItem.getAttribute("Desc")                    vArray(1, i) = oItem.getAttribute("Qty")                    vArray(2, i) = oItem.getAttribute("Price")                    vArray(3, i) = oItem.getAttribute("Disc")                    Next                    m_Data.ProdInfo = vArray                    End Sub                    Public Sub MakeInvoice(sTemplate As Variant, Optional bSave As Variant)                    Dim oWord As Object                    Dim oDoc As Object                    Dim oTable As Object                    If IsMissing(bSave) Then bSave = False                    'Open the document as read-only.                    Set oWord = CreateObject("Word.Application")                    Set oDoc = oWord.Documents.Open(sTemplate, , True)                    'Fill in the bookmarks.                    oDoc.Bookmarks("Customer_Info").Range.Text = m_Data.CustInfo                    oDoc.Bookmarks("Customer_ID").Range.Text = m_Data.CustID                    oDoc.Bookmarks("Order_ID").Range.Text = m_Data.OrderID                    oDoc.Bookmarks("Order_Date").Range.Text = m_Data.OrderDate                    'Fill in the table with the product information.                    '** Note that the table starts out with three rows -- the first row                    '   contains headers for the table, the second row is for                    '   the first set of product data, and the third row contains a total.                    '   New rows are added for additional products before the "total row".                    Set oTable = oDoc.Tables(1)                    Dim r As Integer, c As Integer                    For r = 1 To UBound(m_Data.ProdInfo, 2) + 1                    If r > 1 Then oTable.Rows.Add (oTable.Rows(oTable.Rows.Count))                    For c = 1 To 4                    oTable.Cell(r + 1, c).Range.Text = _                    m_Data.ProdInfo(c - 1, r - 1)                    Next                    oTable.Cell(r + 1, 5).Formula _                    "=(B" & r + 1 & "*C" & r + 1 & ")*(1-D" & r + 1 & ")", _                    "#,##0.00"                    Next                    'Update the field for the grand total and protect the document.                    oTable.Cell(oTable.Rows.Count, 5).Range.Fields.Update                    oDoc.Protect 1 'wdAllowOnlyComments=1                    If bSave Then                    'Save the document as "c:\invoice.doc" and quit Word.                    Dim nResult As Long                    nResult = MsgBox("Are you sure you wish to create the document" & _                    " ""c:\invoice.doc""? If this document already exists, " & _                    "it will be replaced", vbYesNo, "AutomateWord")                    If nResult = vbYes Then oDoc.SaveAs "c:\invoice.doc"                    oDoc.Close False                    oWord.Quit                    Else                    'Make Word visible.                    oWord.Visible = True                    End If                    End Sub                    

            从网页使用 ActiveX 组件

            Autoword1.htm 说明了您如何才能使用 GetData 方法让 ActiveX 组件检索客户端订单数据并生成文档。

            Autoword1.htm
                                                                                                                                    Enter an order id between 10248 and 11077 and click the button to view the invoice for the order:                    

            Autoword1.htm 中的脚本使用 ActiveX 组件来在浏览器之外显示已完成的文档。您也可以选择保存已完成的文档并在浏览器中显示它,不过,这样做会要求将 Word 文档保存到磁盘。该组件可以在客户端的本地驱动器上将文档另存为 C:\Invoice.doc。由于 ActiveX 组件标记为可以安全编写脚本,所以会提示客户端确认保存。

            如果您希望在浏览器中显示已完成的文档,请将 Autoword1.htm 中对 MakeInvoice 的调用更改为以下内容:
                  AutoWord.MakeInvoice "http://YourWebServer/invoice/invoice.doc", True                    window.navigate "c:\invoice.doc"                    
            Autoword2.htm 说明了您如何才能使用 SendData 方法将订单数据作为 DOMDocument 对象发送到 ActiveX 组件,以生成已完成文档。DOMDocument 是从驻留在网页上的 XML 数据岛中创建的。为了使 ActiveX 组件可以正确地处理调用方发送的订单信息,XML 必须结构完好,以便组件可以将它解释为订单信息。

            Autoword2.htm
                                                                                                                                                                                                10700                    10/10/2000                    SAVEA                    Save-a-lot                    Markets Boise ID 83720                    USA                                                                                                                                                                                                                                                
            在 Autoword1.htm 和 Autoword2.htm 这两者中,您应该注意,ActiveX 组件是通过 标签而不是通过 CreateObject 函数实例化的。使用 标签的目的是,让尚未安装 ActiveX 组件的用户可以自动下载该组件。如果用户访问这些页面中的某一页,在未安装该组件时,会从 URL 上 CODEBASE 属性中指示的压缩包 (CAB) 文件中下载该组件。根据 Internet Explorer 中用户的安全设置的不同,他们可能会首先看到提示以确认下载。

            注意:Autoword.exe 中包含的 CAB 文件是使用 Visual Basic 的“打包和展开向导”创建的。包中的 ActiveX 组件标记为可安全处理脚本和初始化,但它没有经过数字签名。

            有关创建 Internet 组件下载、数字签名以及将组件标记为可安全处理脚本和初始化的更多信息,请参阅以下 Microsoft Developer Network (MSDN) 网站: Signing and Checking Code with Authenticode(使用验证码对代码进行签名并检查代码)
            http://msdn.microsoft.com/workshop/security/authcode/signing.asp (http://msdn.microsoft.com/workshop/security/authcode/signing.asp)

            Safe Initialization and Scripting for ActiveX Controls(ActiveX 控件的安全初始化和脚本执行)
            http://msdn.microsoft.com/workshop/components/activex/safety.asp (http://msdn.microsoft.com/workshop/components/activex/safety.asp) 回到顶端