将程序集链接到 Word 或 Excel 文件

如何:将程序集链接到 Word 或 Excel 文件

包含托管代码扩展的 Microsoft Office 2003 文档需要具有指向它的关联程序集的路径。此路径及程序集名称存储于文档的自定义属性 _AssemblyLocation0_AssemblyName0 中。路径必须具有以下格式之一:

  • 绝对路径 (C:\Assemblies\)
  • 相对路径 (\Assemblies\)
  • HTTP (http://www.contoso.com/assemblies/)
  • UNC (\\AssemblyServer\Assemblies\)

有关有效路径的更多信息,请参见“程序集链接位置”属性

如果程序集名称或位置的长度超过 255 个字符,将按编号顺序在 Office 文档中添加更多的自定义属性以包含超出的字符。例如,如果程序集位置包含 300 个字符,则需要两个属性:_AssemblyLocation0_AssemblyLocation1

在 Word 或 Excel 文件中设置程序集名称和位置

可在 Word 或 Excel 文件的自定义属性中设置程序集名称和位置,有以下三种方法:

  • 在 Visual Studio .NET 中使用“属性”窗口(建议)。
  • 运行用于设置属性的脚本。
  • 在文档中手动设置属性,既可以在 Windows 资源管理器中也可在应用程序自身中进行设置。

不是所有文档格式都支持自定义属性。例如,如果将文档保存为文本文件 (.txt),则不保存程序集信息。

在 Visual Studio .NET 中使用“属性”窗口将程序集链接到文件

  • 在生成解决方案之前,在“属性”窗口中将“程序集链接位置”项目属性设置为程序集的位置。

在 Visual Studio .net 项目中使用“属性”窗口有助于在项目与部署的解决方案之间保持同步。

使用脚本将程序集链接到文件

通过脚本使用 Persistence Control 更改文档中的 _AssemblyLocation0 自定义属性。

  1. 创建一个脚本,以实例化该控件并调用希望使用的方法。有关方法的详细信息,请参见 Persistence Control 方法

    例如,使用 JScript:

    // JScript
        if (WScript.Arguments.Length < 3)
        {
        print("Usage: SetAssemblyNameAndLocation <document> <assembly> <location>")
        print("")
        print("  <document> is the name of a Word document or Excel spreadsheet")
        print("  <assembly> is the name of the assembly, without the extension")
        print("  <location> can be a relative path, and should end in a slash")
        WScript.Quit(1)
        }
        var doc = WScript.Arguments(0)
        var asm = WScript.Arguments(1)
        var loc = WScript.Arguments(2)
        print("Updating '" + doc + "', setting assembly='" + asm + "' and location='" + loc + "'")
        try
        {
        var control = new ActiveXObject("OfficeToolkit.Persistence")
        control.SetAssemblyNameAndLocation(doc, asm, loc)
        print("Success!")
        }
        catch(e)
        {
        print("Error:")
        print(e.number + ": " + e.description)
        WScript.Quit(2)
        }
        function print(s)
        {
        WScript.Echo(s)
        }

    以下代码使用 VBScript 演示同一个示例:

    ' VBScript
        If (WScript.Arguments.Length < 3) Then
        print "Usage: SetAssemblyNameAndLocation <document> <assembly> <location>"
        print ""
        print "  <document> is the name of a Word document or Excel spreadsheet"
        print "  <assembly> is the name of the assembly, without the extension"
        print "  <location> can be a relative path, and should end in a slash"
        WScript.Quit 1
        End If
        Dim doc
        Dim asm
        Dim loc
        doc = WScript.Arguments(0)
        asm = WScript.Arguments(1)
        loc = WScript.Arguments(2)
        print "Updating '" & doc & "', setting assembly='" & asm & _
        "' and location='" & loc & "'"
        On Error Resume Next
        Dim control
        Set control = CreateObject("OfficeToolkit.Persistence")
        control.SetAssemblyNameAndLocation doc, asm, loc
        If (Err.Number <> 0) Then
        print "Error:"
        print Err.Number & ": " & Err.Description
        WScript.Quit 2
        End If
        On Error Goto 0
        print "Success!"
        Sub print(s)
        WScript.Echo s
        End Sub
  2. 使用有具体含义的名称保存文件,并在文件名末尾添加扩展名 .js(使用 JScript)或 .vbs(使用 VBScript)。
  3. 打开命令提示。
  4. 将目录更改为保存脚本文件的位置。
  5. 键入脚本文件的名称(不带扩展名),后跟三个参数(按以下顺序):
    <script file> <document name> <assembly name> <deployment location>
  6. ENTER 键。

此示例脚本对加密的工作簿或非本机文件类型(如 HTML 或 MHT)无效。有关更多信息,请参见安全部署中的“部署加密的 Excel 工作簿”。

通过在 Word 或 Excel 中手动更改属性来链接程序集

  1. 在 Word 或 Excel 中打开文档。
  2. 打开“文件”菜单,单击“属性”。

    文档或工作簿的“属性”对话框随即打开。

  3. 单击“自定义”选项卡。
  4. 在“属性”列表中,选择 _AssemblyLocation0

    用该属性的数据填充位于对话框顶部的字段。

  5. 将包含程序集的目录的路径添加到“值”字段。
    注意   Windows 资源管理器在“属性”对话框的“值”字段中不接受长度超过 255 个字符的值。
  6. 在“属性”列表中选择 _AssemblyName0
  7. 将程序集的名称(不带文件扩展名)添加到“值”字段。
  8. 单击“确定”。

通过使用 Windows 资源管理器手动更改属性来链接程序集

  1. 打开 Windows 资源管理器,浏览至文档或工作簿,然后选中它。
  2. 打开“文件”菜单,单击“属性”。

    文档或工作簿的“属性”对话框随即打开。

  3. 单击“自定义”选项卡。

    “自定义”选项卡上的“属性”列表中不显示自定义文档属性,但您仍可通过手动键入名称和值来更改属性值。

    注意   如果打开了没有自定义属性的文档或工作簿,“自定义”选项卡不会显示在任何文档或工作簿的“属性”对话框中。如果在“属性”对话框中看不到“自定义”选项卡,请关闭所有打开的文档或工作簿,然后重新启动此过程。
  4. 在“名称”字段中,键入 _AssemblyLocation0
  5. 在“值”字段中键入包含程序集的目录的路径。
    注意   Windows 资源管理器在“属性”对话框的“值”字段中不接受长度超过 255 个字符的值。
  6. 在“名称”字段中键入 _AssemblyName0
  7. 在“值”字段中键入程序集的名称(不带文件扩展名)。
  8. 单击“确定”。
posted @ 2007-06-03 22:25  过河卒A  阅读(2228)  评论(2编辑  收藏  举报