Visual Studio 環境中的 Office 專案Office projects in the Visual Studio environment

開發 Microsoft Office 專案的方式與在 Visual Studio 中開發其他類型的專案 (例如 Windows Forms 專案) 類似。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. 這個節點的名稱與主應用程式相同 (例如 WordExcelOutlook)。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 專案也包含名稱為 專案名稱 _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

您可以將「 主控制項 」(Host Control) 和 Windows Forms 控制項,從 Visual Studio [ 工具箱 ] 拖曳至文件設計介面。You can drag host controls and Windows Forms controls from the Visual Studio Toolbox onto the document design surface. 主控制項是 Office 物件 (例如 Word 內容控制項和 Excel 範圍) 的特製化版本,可用在以 Visual Studio 建立的 Office 專案中。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.

如需詳細資訊,請參閱 主專案和主控制項總覽Office 檔上的 Windows forms 控制項總覽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. 每個程式碼檔都包含一個產生的「 主項目 」(Host Item) 類別,這個類別代表活頁簿或工作表。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. 程式碼檔會包含一個產生的「 主項目 」(Host Item) 類別,這個類別代表文件。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 環境中開啟文件時,文件一律會處於「 設計模式」(Design Mode)。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