在 Visual Studio 环境中的 office 项目Office projects in the Visual Studio environment

Microsoft Office 项目的开发体验与 Visual Studio 中其他类型的项目(如 Windows 窗体项目)相似。Microsoft Office projects have a development experience that is similar to other types of projects in Visual Studio, such as Windows Forms projects. 当你创建或打开 Office 项目时,项目项会出现在 “解决方案资源管理器” 中。When you create or open an Office project, the project items appear in Solution Explorer. 对于文档级项目,文档(即 Word 文档或 Excel 工作簿)将在 Visual Studio 中打开,该文档的行为就如同一个可视化设计器。For document-level projects, the document (that is, the Word document or Excel workbook) opens in Visual Studio, and the document behaves as a visual designer.

适用于: 本主题中的信息适用于文档-级别项目和 VSTO 添加-在项目中。Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects. 请参阅按 Office 应用程序和项目类型提供的功能See Features available by Office application and project type.

在解决方案资源管理器中的项目项Project items in Solution Explorer

在文档级项目中, “解决方案资源管理器” 显示下列默认项:In a document-level project, Solution Explorer displays the following default items:

  • 由项目自定义的文档、工作簿和表的节点。Nodes for the document, workbook, and sheets that are customized by the project. 这些节点用作与文档、工作簿和表相关联的代码文件的容器。These nodes serve as containers for the code files that are associated with the document, workbook, and sheets.

  • 与由项目自定义的文档、工作簿和表相关联的代码文件。Code files that are associated with the document, workbook, and sheets that are customized by the project. 在 Word 项目中,代码文件与 Word 文档或模板相关联。In Word projects, code files are associated with the Word document or template. 在 Excel 项目中,代码文件与 Excel 工作簿或模板相关联,并与工作簿或模板中的每个工作表和图表工作表相关联。In Excel projects, code files are associated with the Excel workbook or template, and with each worksheet and chart sheet in the workbook or template.

  • 你不打算直接编辑的隐藏项目文件。Hidden project files that you are not intended to edit directly. 有关详细信息,请参阅隐藏项目文件For more information, see Hidden project files.

    在 VSTO 外接程序项目中,“解决方案资源管理器” 显示下列默认项:In a VSTO Add-in project, Solution Explorer displays the following default items:

  • 应用程序节点。The application node. 此节点的名称与宿主应用程序相同,例如 “Word”“Excel”“Outlook”This node has the same name as the host application, such as Word, Excel, or Outlook. 应用程序节点包含 ThisAddIn 代码文件。The application node contains the ThisAddIn code file. 它还提供 “宿主项的命名空间” 属性。It also provides the Namespace for Host Item property. 有关此属性的详细信息,请参阅Office 项目中的属性For more information about this property, see Properties in Office projects.

  • ThisAddIn 代码文件。The ThisAddIn code file. 此文件包含 VSTO 外接程序的 ThisAddIn 生成类。This file contains the generated ThisAddIn class for your VSTO Add-in. 有关此类的详细信息,请参阅程序 VSTO 外接程序For more information about this class, see Program VSTO Add-Ins.

  • 你不打算直接编辑的隐藏项目文件。Hidden project files that you are not intended to edit directly. 有关详细信息,请参阅隐藏项目文件For more information, see Hidden project files.

临时证书Temporary Certificates

Office 项目还包括名为 Project Name_TemporaryKey.pfx 的临时证书。Office projects also include a temporary certificate named Project Name_TemporaryKey.pfx. 此证书用于在开发期间对项目的应用程序和部署清单进行签名。This certificate is used to sign the application and deployment manifests for the project during development. 有关详细信息,请参阅向 Office 解决方案授予信任保护 Office 解决方案For more information, see Grant trust to Office solutions and Secure Office solutions.

隐藏的项目文件Hidden project files

默认情况下,某些项目文件是隐藏的。Several project files are hidden by default. 这些文件由 Visual Studio 生成,它们因项目类型而异。These files are generated by Visual Studio, and they differ by project type. 若要显示隐藏文件,请在 “解决方案资源管理器” 中,单击 “显示所有文件”To display the hidden files, click Show All Files in Solution Explorer.

不要修改隐藏项目文件。Do not modify the hidden project files. 不支持直接更改这些文件,否则可能损坏你的项目。Changing these files directly is not supported, and might corrupt your project. 每当文档中发生某些更改时,就会重新生成隐藏的项目文件。The hidden project files are regenerated whenever certain changes occur in the document. 如果你对隐藏的项目文件进行手动更改,这些更改将在下次文件重新生成时丢失。If you make manual changes to a hidden project file, those changes are lost when the file is regenerated.

在文档级项目中的文档设计器Document designer in document-level projects

Excel 和 Word 的文档级项目提供一个设计器,该设计器承载与 Visual Studio 中的项目相关联的文档。Document-level projects for Excel and Word provide a designer that hosts the document that is associated with your project in Visual Studio. 设计器使你可以在不必离开 Visual Studio 环境的情况下修改文档。The designer enables you to modify the document without having to go outside the Visual Studio environment.

若要在设计器中打开文档,请在 “解决方案资源管理器” 中双击与该文档相关联的代码文件。To open a document in the designer, double-click the code file in Solution Explorer that is associated with the document. 例如,若要在 Excel 项目的设计器中打开工作表 Sheet1 ,请双击 Sheet1 代码文件。For example, to open the worksheet Sheet1 in the designer in an Excel project, double-click the Sheet1 code file.

在设计器中修改文档时,可以利用 Office 应用程序的原有功能。When you modify the document in the designer, you can leverage the native functionality of the Office application. 例如,可以在文档或工作表中键入文本,也可以使用功能区执行诸如添加表格或图表之类的任务。For example, you can type text in the document or a worksheet, or you can use the Ribbon to perform tasks like adding a table or chart. 默认情况下,键盘快捷键映射默认为 Visual Studio 映射。By default, keyboard shortcut mapping defaults to the Visual Studio mapping. 若要转而使用 Office 键盘快捷键映射,请更改 “工具” 菜单上 “选项” 对话框中 “Microsoft Office 键盘设置” 节点下的设置。To use Office keyboard shortcut mappings instead, change the settings under the Microsoft Office Keyboard Settings node in the Options dialog box on the Tools menu.

文档中的控件Controls on Documents

可以将宿主控件 和 Windows 窗体控件从 Visual Studio “工具箱” 拖到文档设计图面上。You can drag host controls and Windows Forms controls from the Visual Studio Toolbox onto the document design surface. 宿主控件是可用于通过 Visual Studio 创建的 Office 项目的特殊版本的 Office 对象,例如 Word 内容控件和 Excel 范围。Host controls are specialized versions of Office objects, such as Word content controls and Excel ranges, that can be used in Office projects created by using Visual Studio. 宿主控件具有在对应 Office 对象中不可用的附加功能,例如数据绑定和附加事件。Host controls have additional features that are not available in the corresponding Office objects, such as data binding and additional events.

有关详细信息,请参阅主机项和主机控件概述Windows 窗体控件在 Office 文档概述For more information, see Host items and host controls overview and Windows forms controls on Office documents overview.

Excel 工作表和设计器中的工作簿Excel worksheets and workbooks in the Designer

当在设计器中打开工作表时,可以采用与在 Excel 中直接打开进行修改的相同方式来修改工作表。When you open a worksheet in the designer, you can modify the worksheet in the same way that you can when it is open directly in Excel. 当双击工作表单元格时,该单元格将切换到编辑模式。If you double-click a worksheet cell, the cell changes to edit mode. 如果您双击包含宿主控件的单元格,此时会打开代码编辑器和 Visual Studio 生成控件的默认事件处理程序。If you double-click a cell that contains a host control, the Code Editor opens and Visual Studio generates the default event handler for the control. 若要导航到其他工作表,可以在设计器底部单击工作表选项卡。To navigate to other worksheets, you can click the worksheet tabs at the bottom of the designer.

当在设计器中打开工作簿时,并没有设计图面。When you open the workbook in the designer, there is no design surface. 工作簿的设计视图是填充设计器的大型组件栏。The design view for the workbook is a large component tray that fills the designer.

工作簿和工作簿中的每个工作表都有相关联的代码文件。The workbook and each sheet in the workbook has an associated code file. 每个代码文件包含一个已生成的代表工作簿或工作表的宿主项 类。Each code file contains a generated host item class that represents the workbook or sheet. 有关详细信息,请参阅通过使用扩展的对象自动化 ExcelFor more information, see Automate Excel by using extended objects.

在设计器中的 Word 文档Word documents in the designer

当在设计器中打开文档时,可以采用与在 Word 中直接打开进行修改的相同方式来修改文档。When you open the document in the designer, you can modify the document in the same way that you can when it is open directly in Word. 如果双击文档中的一个单词时,将选中该单词。If you double-click a word in the document, that word is selected. 但是,如果该单词位于宿主控件内部,便会打开代码编辑器,Visual Studio 生成控件的默认事件处理程序。However, if the word is inside a host control, the code editor opens and Visual Studio generates the default event handler for the control.

文档具有关联的代码文件。The document has an associated code file. 代码文件包含一个已生成的代表文档的宿主项 类。The code file contains a generated host item class that represents the document. 有关详细信息,请参阅文档宿主项For more information, see Document host item.

与运行时模式下的设计模式Design mode vs. runtime mode

当文档在 Visual Studio 环境中打开时,它始终处于设计模式 。When a document is open in the Visual Studio environment, it is always in design mode. 某些任务(例如将宿主控件拖动到文档图面)只能在设计模式下执行。Some tasks, such as dragging a host control to the document surface, can be performed only design mode.

若要查看的文档中运行时模式,则必须打开该应用程序和 Visual Studio 外部文档。To view the document in runtime mode, you must open the application and the document outside Visual Studio. 也可以生成并运行项目,这样便会在 Visual Studio 外自动打开文档和应用程序。You can also build and run the project, which will automatically open the document and application outside Visual Studio.

代码编辑器Code Editor

通过代码编辑器,可以查看和修改解决方案中的可见代码文件。The Code Editor enables you to view and modify the visible code files in your solution. 这些文件包含定义解决方案行为的代码。These files contain the code that defines the behavior of your solution.

有关代码编辑器中的详细信息,请参阅代码和文本编辑器中编写代码For more information about the Code Editor, see Write code in the code and text editor. 有关如何在 Office 项目中编写代码的详细信息,请参阅在 Office 解决方案中编写代码For more information about how to write code in Office projects, see Write code in Office solutions.

“属性”窗口Properties window

“属性” 窗口显示 “解决方案资源管理器” 中选择的项目项的属性,以及设计器中选择的 UI 元素的属性,具体如文档级项目中的控件或文档的属性。The Properties window displays properties for project items that are selected in Solution Explorer, and for UI elements that are selected in the designer, such as controls or the document in a document-level project. 有些属性是特定于应用程序和文档的,而有些属性则是所有项目共有的。Some properties are specific to the application and document, and some properties are the same across all projects.

“数据源”窗口Data Sources window

可以在文档级 Office 项目中使用 “数据源” 窗口将数据源拖到文档上,以及创建绑定到数据源的控件。You can use the Data Sources window in document-level Office projects to drag a data source onto your document and create a control that is bound to the data source. 有关详细信息,请参阅将控件绑定到 Visual Studio 中的数据For more information, see Bind controls to data in Visual Studio.

请参阅See also