Office Documents in the Visual Studio Environment Overview
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.
Microsoft Office version
For more information, see Features Available by Application and Project Type.
Working with Microsoft Office documents in Visual Studio Tools for Office is very similar to working with Windows Forms. When you start a Visual Studio Tools for Office project, the Office document (Microsoft Office Word document or Microsoft Office Excel workbook) opens inside of Visual Studio, and the document behaves as a visual designer.
There are two views in the designer: Design view and Code view. Some of the functionality of these two views is the same no matter which Office application is open in the Visual Studio environment. Some functionality depends on whether Word or Excel is currently open.
Design view is the graphical view of the document and application. The Office document opens in the designer. The designer enables you to access the Office Ribbon groups, menus, and toolbars, and to design the document without having to go outside of the Visual Studio environment. If your project uses Microsoft Office 2003 as the target version, Word and Excel menus are merged with the Visual Studio menus, but the toolbars are in the designer just above the document. For more information, see Office Menus in the Visual Studio Environment. If your project uses the 2007 Microsoft Office system as the target version, the Ribbon appears in the designer just above the document.
You can edit and modify a document or workbook by using the native functionality of the Office application, for example by using Undo, writing Excel formulas, and using the Find feature of Office. You can also use the Visual Studio tool windows and editors to customize the document or workbook using managed code. Keyboard shortcut mapping defaults to the Visual Studio mapping, but you can include Office mappings by changing the setting in the Options dialog on the Tools menu, under the Microsoft Office Keyboard Settings node.
You can drag controls from the Visual Studio Toolbox onto the document design surface, and modify the control properties using the Properties window. Double-clicking the control will open the Code view with an automatically generated event handler. Controls that are not visible in the document appear in the component tray during design time.
Word and Excel have a design mode that enables customizations that cannot be performed during the normal run-time mode. When a document is open in the Visual Studio environment, it is always in design mode. To view the document in run-time mode, you must open the application and the document outside of Visual Studio. You can also build and run the project, which will automatically open the document and application outside of Visual Studio.
The Code Editor associated with the document in the designer is the same as the Code Editor you find behind Windows Forms. For more information, see Code and Text Editor.
Each Excel worksheet has a design view that is a normal worksheet, and the design view of the workbook is a large component tray that fills the designer. The worksheet tabs appear at the bottom of the worksheets, and you can navigate from one to another by clicking the tabs as you would in Excel. There is also a code file associated with each worksheet and with the workbook.
When you write code behind a worksheet or the workbook, your code is local to that object. However, you can access the other objects by using Globals in your code. For more information, see Global Access to Objects in Visual Studio Tools for Office Projects.
When you double-click a normal worksheet cell, the cell switches to edit mode. When you double-click a cell that contains a host control, the code editor opens and the default event handler is generated. For more information about host controls, see Host Items and Host Controls Overview.
There is only one design view for Word documents, which is the document itself. When you double-click the document surface, your cursor moves to that location as usual in Word. Similarly, when you double-click a word, that word is selected. However, if the word is inside a host control, the code editor opens and the default event handler for the control is generated. For more information about host controls, see Host Items and Host Controls Overview.
If your project uses Microsoft Office 2003 as the target version, Word 2003 and Excel 2003 toolbars appear inside the designer and are fully functional. The set and layout of the toolbars is the same as when you open Word and Excel outside of Visual Studio.
Toolbar customization works in the following ways inside the Visual Studio environment:
You can add and remove toolbars by using the View menu or by right-clicking the toolbar in the designer.
You cannot dock Word and Excel toolbars into the Visual Studio set of toolbars.
You cannot customize toolbar layout within a toolbar group by pressing the ALT key, and then selecting the control and moving it.
Toolbar customization such as adding, removing, or moving toolbars lasts only as long as the customized window is open. The customizations are gone the next time the same document, or any other document, is opened. When a document opens, the toolbar settings are identical to the settings of the stand-alone application.
You cannot move toolbar commands into the menus.
You use Solution Explorer the same way as you would in other types of projects, such as Windows Forms.
Visual Studio Tools for Office projects include the following items:
References to Office and the primary interop assemblies. These only appear in Solution Explorer in C# projects; in Visual Basic, they are listed in the project properties.
For Excel, each worksheet and chart, which appear as separate items in Solution Explorer.
Hidden implementation files. To view these files, click the Show All Files button. For more information, see Hidden Project Files in Solution Explorer.
Worksheets that have been hidden in Excel. These appear in Solution Explorer, but not in the designer. For more information, see Hidden Worksheets.
You can add other regular project items such as Windows Forms and code files. For more information about Solution Explorer, see Using Solution Explorer.
Just as with Windows Forms, you can use the Data Sources window in your Visual Studio Tools for Office projects to drag a data source onto your document and automatically include a control that is bound to the data source. For more information, see Displaying Data on Forms in Windows Applications.