Office 解决方案开发概述 (VSTO)Office solutions development overview (VSTO)

通过将 Microsoft Office 用作解决方案的前端,你可以利用熟悉的 Microsoft Office 用户界面和工具,例如 Word 中的文字处理功能、Excel 的数据分析功能、数据分析功能和 Outlook 的电子邮件管理功能。By using Microsoft Office as the front end for solutions, you can take advantage of the familiar Microsoft Office user interfaces and tools such as the word processing features in Word, the data analysis features of Excel, and the e-mail management features of Outlook. 你可以在 Visual Studio 中开发解决方案以自定义 Office 应用程序并添加业务流程所需的特定功能。You can develop solutions in Visual Studio to customize Office applications and add the specific features you need for your business processes. 例如,你可以将 Word 转化为协定生成器,收集可进行编辑或不可编辑的预存在部件外部的协定。For example, you can turn Word into a contract generator that assembles contracts out of pre-existing parts that can be made editable or not editable. 通过使用 Excel,你可以为不同项目创建定制的自动化预算工作表。With Excel, you can create an automated budget worksheet customized for different projects. 你的用户也可以脱机使用 Office 解决方案,使复杂的解决方案比起在使用基于 Web 的体系结构时更简单实用。Your users can also take office solutions offline, which makes complex solutions more practical than they would be if you use a web-based architecture.

本主题概述了你可以通过 Visual Studio 中 Office 开发人员工具提供的 Visual Studio Tools for Office (VSTO) 模板创建的 Office 解决方案的类型。This topic provides an overview of the types of Office solutions that you can create by using the Visual Studio Tools for Office (VSTO) templates available in the Office developer tools in Visual Studio. 有关如何使用 Office 进行开发的常规信息,请参阅Office 开发人员中心For general information about how to develop with Office, see the Office developer center.

选择 Office 项目类型Choose an Office project type

Visual StudioVisual Studio 提供了基于 VSTO 的 Office 开发的以下类型项目模板:provides the following types of project templates for VSTO-based Office development:

  • 文档级自定义项 与特定文档相关联。Document-level customizations are associated with a specific document.

  • VSTO Add-ins 与应用程序自身相关联。VSTO Add-ins are associated with the application itself.

    若要确定这些项目类型中的哪一个最适合你的解决方案,可思考一下你想要代码仅在打开特定文档时运行还是想只要运行应用程序就提供代码。To decide which of these project types is best for your solution, think about whether you want your code to run only when a specific document is open, or whether you want the code to be available whenever the application is running. 有关项目模板的详细信息,请参阅Office 项目模板概述For more information about the project templates, see Office project templates overview.

    可以创建的项目类型取决于在开发计算机上已安装的 Office 应用程序。The types of projects you can create depend on which Office applications you have installed on the development computer. 有关详细信息,请参阅按 Office 应用程序和项目类型提供的功能For more information, see Features available by Office application and project type.

文档级自定义项Document-level customizations

文档级自定义项包含与 Microsoft Office Word 或 Microsoft Office Excel 中的单个文档、工作簿或模板相关联的程序集。Document-level customizations consist of an assembly that is associated with a single document, workbook, or template in Microsoft Office Word or Microsoft Office Excel. 打开关联的文档时,就会加载程序集。The assembly is loaded when the associated document is opened. 仅当打开关联的文档时,才提供创建的自定义项中的功能。Features in customizations that you create are available only when the associated document is open. 自定义项不能进行应用程序范围内的更改,例如打开任何文档时显示新菜单项或功能区选项卡。Customizations cannot make application-wide changes, such as displaying a new menu item or ribbon tab when any document is open.

Visual StudioVisual Studio 包括可帮助你创建文档级自定义项的工具。includes tools to help you create document-level customizations. 你自定义的文档在 Visual StudioVisual Studio中作为设计界面承载,以便在其上通过拖放控件设计文档。The document that you customize is hosted as a design surface in Visual StudioVisual Studio, which enables you to design the document by dragging and dropping controls onto it. 文档级项目中还提供许多其他 Visual StudioVisual Studio 功能,例如“Windows 窗体”控件、拖放数据绑定和集成的调试器。Many other Visual StudioVisual Studio features are available in document-level projects, such as Windows Forms controls, drag-and-drop data binding, and an integrated debugger.

有关自定义项的详细信息,请参阅以下主题:For more information about customizations, see the following topics:

VSTO 外接程序VSTO Add-ins

VSTO 外接程序包含与 Microsoft Office 应用程序相关联的程序集。VSTO Add-ins consist of an assembly that is associated with a Microsoft Office application. 通常情况下,启动相关联的应用程序时,VSTO 外接程序就会运行,但是用户还可以在运行应用程序之后加载 VSTO 外接程序。Typically, the VSTO Add-in runs when the associated application is started, although users can also load VSTO Add-ins after the application is already running. 无论打开哪一个文档,所创建的 VSTO 外接程序中的功能都可用于应用程序自身。Features in VSTO Add-ins that you create are available to the application itself, regardless of which documents are open.

Visual StudioVisual Studio 包括可帮助你创建 VSTO 外接程序的工具。外接程序项目包含一个表示 VSTO 外接程序的自动生成的类。includes tools to help you create VSTO Add-ins. Add-in projects include an automatically generated class that represents the VSTO Add-in. 此类提供的属性和事件可用于访问主机应用程序的对象模型并在加载和关闭 VSTO 外接程序时运行代码。This class provides properties and events that you can use to access the object model of the host application and run code when the VSTO Add-in is loaded and shut down. VSTO 外接程序项目中还提供许多其他 Visual StudioVisual Studio 功能,例如 Windows 窗体和集成的调试器。Many other Visual StudioVisual Studio features are available in VSTO Add-in projects, such as Windows Forms and an integrated debugger.

有关 VSTO 外接程序的详细信息,请参阅以下主题:For more information about VSTO Add-ins, see the following topics:

通过使用主互操作程序集自动化 Office 应用程序Automate Office applications by using primary interop assemblies

通过编写访问应用程序对象模型的代码,你可以以编程方式将 Office 应用程序的功能合并到你的解决方案。You can programmatically incorporate the features of an Office application into your solution by writing code that accesses the application's object model. 对象模型是通过各种属性和方法公开功能的类的排列。Object models are an arrangement of classes that expose functionality through various properties and methods. 每个 Office 应用程序的对象模型都不同。The object model for each Office application is different.

若要使用通过 Visual StudioVisual Studio中的 Office 开发工具创建的解决方案中的 Office 应用程序的对象模型,必须使用该应用程序的主互操作程序集 (PIA)。To use the object model of an Office application from a solution created by using the Office development tools in Visual StudioVisual Studio, you must use the primary interop assembly (PIA) for the application. PIA 可使解决方案中的托管代码与 Office 应用程序基于 COM 的对象模型进行交互。The PIA enables the managed code in your solution to interact with the Office application's COM-based object model.

必须在开发计算机上的全局程序集缓存中安装并注册 Office PIA 才能执行大多数开发任务。You must have the Office PIAs installed and registered in the global assembly cache on your development computer to perform most development tasks. 有关详细信息,请参阅配置计算机以开发 Office 解决方案For more information, see Configure a computer to develop Office solutions. 若要运行 VSTO Office 解决方案,无需在最终用户计算机上安装 Office PIA。The Office PIAs are not required on end-user computers to run VSTO Office solutions. 有关详细信息,请参阅设计和创建 Office 解决方案For more information, see Design and create Office solutions.

有关使用 VSTO Office 解决方案中的 PIA 的详细信息,请参阅以下主题:For more information about using the PIAs in VSTO Office solutions, see the following topics:

在最终用户计算机上运行 Microsoft VSTO Office 解决方案Run Microsoft VSTO Office solutions on end-user computers

当创建 VSTO Office 解决方案时,请考虑部署要求可能影响开发选择的方式。When you create a VSTO Office solution, consider how the deployment requirements might affect your development choices.

部署选项Deployment options

使用 ClickOnce 或 Windows Installer 部署通过使用 Visual StudioVisual Studio中的 Office 开发工具创建的解决方案。Use ClickOnce or Windows Installer to deploy solutions that you create by using the Office development tools in Visual StudioVisual Studio. 通过 ClickOnce 部署,你可以创建自行更新解决方案,以最少的用户交互进行安装和运行。ClickOnce deployment enables you to create self-updating solutions that can be installed and run with minimal user interaction. Windows 安装程序 (.msi) 可以轻松地分发给最终用户计算机,或通过使用 Systems Management Server (SMS) 分发文件。Windows Installer (.msi) files can be easily distributed to end-user computers, or distributed by using Systems Management Server (SMS). 有关部署 VSTO Office 解决方案的详细信息,请参阅部署 Office 解决方案For more information about deploying VSTO Office solutions, see Deploy an Office solution.

安装系统必备组件Install prerequisites

在最终用户可以运行你通过使用 Visual StudioVisual Studio中的 Office 开发工具创建的解决方案之前,必须在其计算机上安装某些必备组件。Before end users can run a solution you create by using the Office development tools in Visual StudioVisual Studio, their computers must have certain prerequisites installed. 如果通过使用 ClickOnce 或通过创建 Windows Installer 文件部署解决方案,则可以使用你的解决方案安装这些必备组件。If you deploy your solution by using ClickOnce or by creating a Windows Installer file, these prerequisites can be installed with your solution. 有关详细信息,请参阅用于部署 Office 解决方案必备组件如何:若要运行 Office 解决方案的最终用户计算机上安装的必备组件For more information, see Office solution prerequisites for deployment and How to: Install prerequisites on end user computers to run Office solutions.

安全性Security

当安装和加载 VSTO Office 解决方案时,将由 Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 对该解决方案强制进行一系列安全性检查。Security for VSTO Office solutions is enforced by a series of checks that the Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime makes when it installs and loads the solution. 这些检查包括验证部署清单的位置是否受信任或用于对部署清单签名的证书是否受信任。These checks include verifying whether the location of the deployment manifest is trusted or whether the certificate used to sign the deployment manifest is trusted. 有关详细信息,请参阅保护 Office 解决方案For more information, see Secure Office solutions.

请参阅See also