Office 项目模板概述Office project templates overview

Visual Studio 中的 Microsoft Office 开发人员工具包括用于创建以下类型的 Office 解决方案的项目模板:The Microsoft Office developer tools in Visual Studio include project templates for creating the following types of Office solutions:

  • 文档级自定义项Document-level customizations

  • VSTO 外接程序VSTO Add-ins

    有关这些类型的 Office 解决方案的详细比较, 请参阅office 解决方案开发概述(VSTO)For a detailed comparison of these types of Office solutions, see Office solutions development overview (VSTO).

    Office 项目模板位于 “新建项目” 对话框中,你可以从 “Visual C#”“Visual Basic” 语言节点的 “Office” 节点下找到该对话框。The Office project templates are available in the New Project dialog box, under the Office node of the Visual C# and Visual Basic language nodes. 每个模板都使用目标应用程序的相应配置来生成项目,包括程序集引用和调试设置。Each template generates a project with the appropriate configuration for the target application, including assembly references and debugging settings.

    每个项目都提供了文件和代码,使你可以开始使用特定类型的解决方案。Each project provides files and code to get you started on a specific kind of solution. 每个项目的生成代码都包括启动和关闭事件处理程序。The generated code for each project includes startup and shutdown event handlers. 可以向这些事件处理程序添加代码,以便在加载解决方案时对其进行初始化,并在卸载解决方案时对其进行清理。You can add code to these event handlers to initialize your solution when it is loaded and to clean up your solution when it is unloaded. 有关详细信息, 请参阅Visual Studio 环境中的 office 项目office 项目中的事件For more information, see Office projects in the Visual Studio environment and Events in Office projects.

Note

Office 开发工具随某些 Visual Studio 版本提供。The Office development tools are included with certain editions of Visual Studio. 有关详细信息, 请参阅配置计算机以开发 Office 解决方案For more information, see Configure a computer to develop Office solutions.

文档级自定义项Document-level customizations

“新建项目” 对话框中的 “Office” 节点提供下列项目模板,可以使用这些模板开始创建 Word 和 Excel 的文档级自定义项:The Office node in the New Project dialog box provides the following project templates to get you started creating document-level customizations for Word and Excel:

  • Word 2013 和 2016 VSTO 文档Word 2013 and 2016 VSTO Document

  • Word 2013 和 2016 VSTO 模板Word 2013 and 2016 VSTO Template

  • Excel 2013 和 2016 VSTO 工作簿Excel 2013 and 2016 VSTO Workbook

  • Excel 2013 和 2016 VSTO 模板Excel 2013 and 2016 VSTO Template

  • Word 2010 VSTO 文档Word 2010 VSTO Document

  • Word 2010 VSTO 模板Word 2010 VSTO Template

  • Excel 2010 VSTO 工作簿Excel 2010 VSTO Workbook

  • Excel 2010 VSTO 模板Excel 2010 VSTO Template

    Word 文档和 Excel 工作簿项目模板提供了代码,使你可以开始创建基于特定文档或工作簿的解决方案。The Word Document and Excel Workbook project templates provide code to get you started creating a solution that is based on a specific document or workbook. 在这些类型的解决方案中,只有在 Word 或 Excel 中打开关联的文档时,代码才运行。In these types of solutions, your code runs only when the associated document is open in Word or Excel.

    “Word 模板”和“Excel 模板”项目模板的工作方式与“Word 文档”和“Excel 工作簿”项目模板相同。The Word Template and Excel Template project templates behave identically to the Word Document and Excel Workbook project templates. 但是,用户可以使用“Word 模板”和“Excel 模板”项目模板轻松地为解决方案中的自定义模板创建新的本地文档或工作簿副本。However, the Word Template and Excel Template project templates makes it easy for users to create new local document or workbook copies of the customized template in your solution. 用户从模板创建的新文档中提供了你的解决方案中的功能。The features in your solution are available from the new document that the user creates from the template.

Note

引用托管代码扩展的 Word 模板不能被用作全局 VSTO 外接程序。如果从 Word 的 Startup 目录加载模板,则不会调用该程序集。Word templates that reference managed code extensions cannot be used as global VSTO Add-ins. The assembly is not called if the template is loaded from the Startup directory of Word. 有关详细信息, 请参阅全局模板和 Excel 外接程序的限制 (bam.xla 文件)For more information, see Limitations of global templates and Excel Add-ins (.xla files).

有关这些项目类型的入门信息,请参阅下列主题:For information about getting started with these project types, see the following topics:

VSTO 外接程序VSTO Add-ins

“新建项目” 对话框中的 “Office/SharePoint” 节点提供下列项目模板,可以使用这些模板开始创建 VSTO 外接程序。The Office/SharePoint node in the New Project dialog box provides the following project templates to get you started creating VSTO Add-ins.

文档与模板解决方案Document vs. template solutions

在围绕 Word 文档或 Excel 工作簿设计解决方案时,必须确定将该文档提供给用户的最佳方式。When you design a solution around a Word document or Excel workbook, you must decide the best way to make that document available to your users.

适用于: 本主题中的信息适用于文档-级别 Excel 和 Word 的项目。Applies to: The information in this topic applies to document-level projects for Excel and Word. 有关详细信息,请参阅按 Office 应用程序和项目类型提供的功能For more information, see Features available by Office application and project type.

某些情况下,你可能希望为每位用户提供一份文档副本。In some situations, you might want to give a copy of a document to each user. 在这种情况下,可以使用 Excel 或 Word 文档项目创建解决方案。In this case, create your solution by using an Excel or Word document project.

而在其他情况下,你可能希望在某个服务器上提供一个模板,以便用户可以打开该模板并保存本地副本作为文档。In other situations you might want to make a template available on a server, so that each user can open the template and save a local copy as a document. 在这种情况下,可以使用 Excel 或 Word 模板项目创建解决方案。In this case, create your solution by using an Excel or Word template project.

比较Comparison

下表概述了文档和模板之间的区别。The following table outlines the differences between documents and templates.

文档Documents 模板Templates
用户可以打开并修改文档,除非该文档被设置为只读。Users can open and modify a document, unless it is set to be read-only. 任何保存过的更改都保留在原文档中。Any saved changes are kept in the original. 用户可以打开模板以创建一个本地副本作为新文档。Users can open a template to create a local copy as a new document. 用户不能修改原模板,除非他们被授予某些特权。They cannot modify the original unless they are given special permissions.
打开时,文档将引发 Open 事件。When opened, the document raises the Open event. 打开时,模板将引发 New 事件。When opened, the template raises the New event.

全局模板和 Excel 外接程序的限制 (bam.xla 文件)Limitations of global templates and Excel Add-ins (.xla Files)

文档、工作簿和模板可能不会像全局模板或 Excel VSTO 外接程序(.xla 文件)那样正常工作。Documents, workbooks, and templates might not work correctly as global templates or Excel VSTO Add-ins (.xla files).

Word 模板Word templates

当 Microsoft Office Word 模板具有托管代码扩展时,如果该模板作为全局模板附加或者从 Word 的 Startup 目录中加载,则不会调用项目程序集。If a Microsoft Office Word template has managed code extensions, the project assembly is not called if the template is attached as a global template or loaded from the startup directory of Word. 此外,文档不识别属于 Office 解决方案的模板的格式。In addition, the document does not recognize the format of a template that is part of an Office solution.

Excel 外接程序 (bam.xla 文件)Excel Add-ins (.xla Files)

没有用于创建 Excel VSTO 外接程序 (bam.xla文件) 的 Office 项目。There is no Office project for creating an Excel VSTO Add-in (.xla file). 可以将工作簿另存为 .xla 文件,但这不是一种受支持的操作,建议你不要这样做。It is possible to save a workbook as an .xla file, but it is not a supported operation and is not recommended. 如果将具有托管代码扩展的工作簿另存为Microsoft Office Excel 外接程序 (*bam.xla) 文件, 则可以在 "外接程序" 对话框中选择将其应用于其他工作簿。If you save a workbook that has managed code extensions as a Microsoft Office Excel Add-In (*.xla) file, you can select it in the Add-Ins dialog box to apply to another workbook. 在某些情况下, 应用 VSTO 外接程序后, 你的代码将在目标工作簿中运行, 但不支持此类 Office 解决方案的使用。In some cases, your code will run in the target workbook after the VSTO Add-in is applied, but such use of the Office solution is not supported.

请参阅See also