在以下步骤中,您将使用 ServerDocument 类编辑文档中嵌入的应用程序清单以指向自定义项程序集的新位置。使用 ServerDocument 编辑嵌入的应用程序清单的代码必须位于与正在部署的文档关联的 Visual Studio Tools for Office 项目程序集以外。
编辑嵌入的应用程序清单
-
创建新的控制台应用程序项目。有关更多信息,请参见 如何:创建解决方案和项目。
-
将对 Microsoft.VisualStudio.Tools.Applications.Runtime.dll 程序集的引用添加到新项目。
-
将下面的 using 语句 (C#) 或 Imports 语句 (Visual Basic) 添加到 Program.cs 或 Module1.vb 代码文件的顶部。
Imports Microsoft.VisualStudio.Tools.Applications.Runtime
using Microsoft.VisualStudio.Tools.Applications.Runtime;
-
在 Main 方法中添加下面的代码以更新嵌入的应用程序清单。将 <full document path> 替换为文档在开发计算机上开发位置中的完整路径 -- 例如,C:\TestDeployDocument\WordDeployment.doc。将 <full assembly path> 替换为程序集在将部署到的文件夹中的完整路径 -- 例如,C:\TestDeployAssembly\WordDeployment.dll。
Dim sd As ServerDocument = Nothing
Try
sd = New ServerDocument("<full document path>")
sd.AppManifest.Dependency.AssemblyPath = _
"<full assembly path>"
sd.Save()
Finally
If Not sd Is Nothing Then
sd.Close()
End If
End Try
ServerDocument sd = null;
try
{
sd = new ServerDocument(@"<full document path>");
sd.AppManifest.Dependency.AssemblyPath =
@"<full assembly path>";
sd.Save();
}
finally
{
if (sd != null)
{
sd.Close();
}
}
-
按 F5 生成并运行项目。控制台应用程序将更新嵌入的应用程序清单中的程序集路径,然后关闭。
注意 |
|---|
| 确保尚未在 Word 或项目设计器的实例中打开文档。如果已经打开该文档,则使用 ServerDocument 尝试访问该文档将失败。 |
-
关闭控制台应用程序项目。
-
打开 %SystemDrive%\TestDeployDocument 文件夹中的文档。将显示一条错误信息,指出当前的 .NET 安全策略不允许该自定义项运行。这是由于您尚未对该程序集授予完全信任。
-
单击“确定”,然后关闭文档。