Share via


Excel Template Project Template

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • Excel 2007

  • Excel 2003

For more information, see Features Available by Application and Project Type.

The Excel Template project template in Visual Basic and Visual C# provides the groundwork for solutions that are based on new or existing Microsoft Office Excel templates. There are different project templates for Excel 2003 templates and Excel 2007 templates. The project templates automatically add the necessary assembly references, files, and generated code to use as a starting point for your solution.

If you open a template to edit it outside of Visual Studio after the solution has been compiled, the template will run the code just as if you had created a new document from the template. Depending on the code, this could alter the template while it opens, before you begin to edit. To prevent the code from running, hold down the SHIFT key while you open the template.

Note

By default, some of the files and references in the following list are hidden. To see them, click Show All Files on the Solution Explorer toolbar. Additionally, there are some files and references not in the following list that are common to many Visual Studio project templates.

Files

Visual Studio Tools for Office generates the following visible files in Excel 2003 and Excel 2007 template projects:

  • Workbook Name.xls - Represents the Excel workbook in the project. Contains all of the worksheets and charts.

  • Sheet1 (.vb file for Visual Basic or .cs file for Visual C#) - A worksheet that provides the design surface and the code for the first worksheet in the workbook. For more information, see Worksheet Host Item.

  • Sheet2 (.vb file for Visual Basic or .cs file for Visual C#) - A worksheet that provides the design surface and the code for the second worksheet in the workbook.

  • Sheet3 (.vb file for Visual Basic or .cs file for Visual C#) - A worksheet that provides the design surface and the code for the third worksheet in the workbook.

  • ThisWorkbook (.vb file for Visual Basic or .cs file for Visual C#) - Contains the design surface and the code for workbook-level customizations. For more information, see Workbook Host Item.

Note

You cannot add additional classes to the code file behind a document, workbook, or worksheet. You must add new files to your project to contain any classes you want to write.

Visual Studio Tools for Office also generates the following additional file when you build an Excel 2007 template project:

Hidden Files

Visual Studio Tools for Office generates several hidden files. Do not modify these files manually; they are regenerated when changes are made to the project and they do not retain manual modifications.

Visual Studio Tools for Office generates the following hidden files:

  • ThisWorkbook.Designer.xml - A read-only declarative representation of the programmable objects in the workbook.

  • ThisWorkbook.Designer (.vb file or .cs file) - A read-only code file that provides helper implementations for objects in the workbook, including code to instantiate objects correctly.

  • Sheetn.Designer.xml - A read-only declarative representation of the programmable objects in the worksheet.

  • Sheetn.Designer (.vb file or .cs file) - A read-only code file that provides helper implementations for objects in the worksheet, including code to instantiate objects correctly.

  • Workbook Name.Designer.xml - A read-only declarative representation of programmable objects such as custom controls that are not part of Visual Studio Tools for Office by default. This file is generated when you add a custom control to a worksheet in the workbook.

  • Workbook Name.Designer (.vb file or .cs file) - A read-only code file that provides helper implementations for custom controls. This file is generated when you add a custom control to a worksheet in the workbook.

Template Code

The following event handlers are included in the project template code files. For more information, see Visual Studio Tools for Office Project Events.

Startup Event Handlers

These event handlers handle the Workbook.Startup and Worksheet.Startup events. These events are raised for each of the worksheets and the workbook after the solution is running and all the initialization code in the assembly has run. Use these event handlers to run code that initializes your solution.

The Startup event handlers in a template solution are called in this order:

  1. ThisWorkbook_Startup.

  2. Sheet1_Startup.

  3. Sheet2_Startup.

  4. Sheet3_Startup.

  5. Other sheets in order.

The order is determined when the project is compiled. If the user rearranges the sheets at run time, it does not change the order that the events are raised the next time the workbook is opened.

Shutdown Event Handlers

These event handlers handle the Workbook.Shutdown and Worksheet.Shutdown events. These events are raised for each of the worksheets and the workbook when the application domain that your code is loaded in is about to unload.

The Shutdown event handlers in a template solution are called in this order:

  1. ThisWorkbook_Shutdown.

  2. Sheet1_Shutdown.

  3. Sheet2_Shutdown.

  4. Sheet3_Shutdown.

  5. Other sheets in order.

References

Visual Studio Tools for Office automatically creates a set of assembly references in Excel template projects. These references include the following assemblies:

See Also

Tasks

How to: Create Visual Studio Tools for Office Projects

Concepts

Limitations of Global Templates and Excel Add-ins (.xla Files)

Assemblies in Office Solutions Overview

Excel Object Model Overview

Other Resources

Managing Solutions, Projects, and Files

Office Project Templates