HOW TO:使用 Visual Studio 專案 Automation 將工作表加入至工作簿
這個範例會將新工作表加入至在 Visual Studio 設計工具中開啟的現有 Microsoft Office Excel 活頁簿。
**適用於:**本主題中的資訊適用於 Excel 2007 和 Excel 2010 的文件層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
範例
Sub AddWorksheet()
' Find the Office project.
Dim vstProject As Project = DTE.Solution.Item(1)
' Find the Excel workbook.
Dim docItem As ProjectItem = _
vstProject.ProjectItems.Item("ExcelWorkbook1.xlsx")
' Activate the Solution Explorer window.
Dim solExpl As Window = _
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)
solExpl.Activate()
' Find and select the Excel workbook.
Dim uiHier As UIHierarchy = solExpl.Object
' The path follows the pattern
' SolutionName\ProjectName\WorkbookName.
Dim item As UIHierarchyItem = _
uiHier.GetItem("ExcelWorkbook1\ExcelWorkbook1.xlsx")
item.Select(vsUISelectionType.vsUISelectionTypeSelect)
' Execute the shortcut menu command to add a new worksheet.
Dim commandGuid As String = _
"{AE97F291-448D-4F9F-93A8-B0D2B78475A4}"
' Set commandID to 1 to add a new worksheet, 2 to add a new chart.
Dim commandID As Integer = 1
Dim customIn As Object
Dim customOut As Object
DTE.Commands.Raise(commandGuid, commandID, customIn, customOut)
End Sub
編譯程式碼
這個範例有下列需求:
在 Visual Studio 設計工具中開啟名為 ExcelWorkbook1 的專案,其中顯示名為 ExcelWorkbook1 的 Excel 活頁簿, 但是方案中必須沒有其他專案開啟。
當您建立 ExcelWorkbook1 專案時,務必不要選取 [新增專案] 對話方塊中的 [為方案建立目錄]。
穩固程式設計
將程式碼加入至 Visual Studio 巨集整合式開發環境 (IDE) 中的模組。
清除 [選項] 對話方塊中的 [永遠顯示方案] 選項。 若要存取這個對話方塊,請在 [工具] 功能表上按一下 [選項],然後選取 [專案和方案] 資料夾並按一下 [一般]。
請參閱
工作
HOW TO:使用 Visual Studio 專案 Automation 變更 Excel 屬性