What's New in Visual Studio 2005 Tools for Office
Janet Robinson, Microsoft Corporation
Published: October 2004
Revised: November 2005
Applies to: Microsoft Visual Studio 2005, Microsoft Visual Studio 2005 Tools for the Microsoft Office System, Microsoft Office Excel 2003, Microsoft Office Word 2003, Microsoft Office InfoPath 2003, Microsoft Office Outlook 2003
Summary: Discover new features in Visual Studio 2005 Tools for Office, including support for InfoPath and Outlook, a visual designer for Word and Excel, host controls, programmable task pane support, visual data binding, data caching, and server-side programming. (11 printed pages)
Microsoft Visual Studio 2005 Tools for the Microsoft Office System (Visual Studio 2005 Tools for Office) brings significant enhancements to the development of business solutions based on Microsoft Office Word 2003, Microsoft Office Excel 2003, Microsoft Office InfoPath 2003, and Microsoft Office Outlook 2003. Building on the foundation provided by the previous version, Visual Studio 2005 Tools for Office addresses some of the biggest challenges that Office solution developers encounter today, including separation of data and view elements, server-side and offline scenarios, and seamless integration with the Visual Studio toolset.
Support for InfoPath means you can add business logic to your form using managed code instead of script. Managed code allows you to use the Microsoft .NET Framework common language runtime (CLR) to write code in Microsoft Visual C# or Microsoft Visual Basic, and to make calls into the .NET Framework class library for advanced tasks such as digital signature processing and encryption/decryption. Visual Studio integration includes F1 Help support, full-featured debugging, and Microsoft IntelliSense in the code editor. IntelliSense provides statement completion, members lists, and inline parameter information on the InfoPath object model. You will learn about InfoPath later in this article.
Support for Outlook add-ins in Visual Studio 2005 Tools for Office is different than for Word, Excel, and InfoPath. You will learn about Outlook add-ins later in this article, or see Introducing Outlook Add-in Support in Visual Studio 2005 Tools for Office for more information.
New Project Templates
In Visual Studio 2005 Tools for Office, the project templates are located under the Visual Basic projects and Visual C# projects folders, in a subfolder named Office. This version of Visual Studio Tools for Office also adds new project templates for Excel templates, InfoPath form templates, and Outlook add-ins.
Figure 1. Visual Studio 2005 Tools for Office project types
Excel solutions based on Visual Studio 2005 Tools for Office no longer have only one code file for the workbook. There are separate code files for each worksheet, and one for the workbook, as shown in Figure 2. Hidden worksheets appear in Solution Explorer, but not in the designer.
Figure 2. Excel code files
Word solutions based on Visual Studio 2005 Tools for Office still have one code file for the document or template.
Visual Designer Integration for Word and Excel
One of the most exciting changes in Visual Studio 2005 Tools for Office is that the Word and Excel application environments are integrated into the Visual Studio designer. You can work with the features of each application to design the look of your document at the same time that you write managed code to extend its capabilities.
Figure 3. Excel workbook open as Visual Studio designer
Working with Word and Excel documents and templates in Visual Studio 2005 Tools for Office is very similar to working with Microsoft Windows Forms. When you start a Visual Studio 2005 Tools for Office project, the Office document (Word document or Excel workbook) opens inside of Visual Studio, and the document behaves as a visual designer. The experience of working with a document in the designer is as close as possible to the experience of working with Windows Forms.
You can edit and modify a document or workbook using the native functionality of the Office application, for example 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 Visual Studio Tools Options dialog box under the Microsoft Office Tools Keyboard Setting node. You can also use the Change Keymapping command on the Visual Studio Word toolbar or Excel toolbar. This command changes the keyboard mapping for only the current project.
Word and Excel toolbars appear inside the designer and are fully functional. The set and layout of the toolbars are the same as when you open Word and Excel outside of Visual Studio. The Word and Excel menus, however, are merged with the Visual Studio menus. Some menus, such as Tools and View, contain many items that are important to have available for both Visual Studio and Office at the same time. To make menu navigation easier, the Office menu items are located in a separate submenu, accessible from the top of the corresponding Visual Studio menu, as shown in Figure 4. Other menus, such as File, only present certain options that you can perform within the Visual Studio environment, and do not make all the Office menu items available.
Figure 4. Merged menus in Visual Studio 2005 Tools for Office
Word and Excel have a design mode that enables you to perform customizations that you cannot perform 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.
For more information, see Office Documents in the Visual Studio Environment Overview.
In Visual Studio 2005 Tools for Office, you can use managed Windows Forms controls and new Word and Excel host controls in your solutions. Managed control hosting within Word and Excel documents, combined with Word and Excel integration within the Visual Studio integrated development environment (IDE), reduces the differences that exist today between Office solution development and Windows solution development. Elements, such as cells, ranges, lists, and bookmarks become first-class controls ("host controls") that are easily accessible in code. Each host control is uniquely identified, enables data binding, and provides a more complete event model.
For more information, see Controls on Office Documents.
Windows Forms Controls
You can drag and drop Windows Forms controls from the Visual Studio Toolbox onto the document design surface, as shown in Figure 5, and modify the control properties using the Properties window, as shown in Figure 6.
Figure 5. Adding a new button to a worksheet
Figure 6. Changing properties of a button
Windows Forms controls generally behave the same on an Office document as they do on a Windows Form or an actions pane, but some differences do exist.
You cannot set the tab order of controls placed on a Word document or Excel worksheet.
You cannot use a GroupBox control to contain other controls on an Office document. If you add radio buttons directly to the document, you must write code to make the radio buttons mutually exclusive. Alternatively, you can add radio buttons to a user control and then add the user control to the document.
Double-clicking the control opens the Code view with an automatically generated event handler. For example, if you double-click a new button named Button1 in a Visual Basic project, the following code is generated.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click End Sub
Controls that are not visible in the document appear in the component tray during design time. For example, if you add an ImageList control to a Word document, it is added to the component tray, as shown in the Figure 7.
Figure 7. ImageList control on a Word document
You can take advantage of new controls, called host controls, which extend some of the built-in objects of Office. These objects now expose events and have data binding capability.
Host controls are controls that extend existing Word and Excel objects. Host controls that are added to Office documents behave like the native Office objects, such as ranges; however, they have additional functionality, including events and data-binding capabilities. For example, when you want to capture the events of a native Range object in Excel, you must trap the change event of the worksheet and then determine if the change occurred in the range you are interested in. With the NamedRange control in Visual Studio 2005 Tools for Office, you can access the change event directly.
The relationship between a host (Word or Excel) and host controls is very similar to the relationship between a Windows Form and Windows Forms controls. Just as you would place a text box control on a Windows Form, you place a NamedRange host control on a worksheet host.
The following table lists the host controls available in Word and Excel projects.
Table 1. Word and Excel host controls
|Word Host Controls||Excel Host Controls|
You can add host controls to your Word documents or Excel worksheets in one of four ways:
Because host controls are extended Office objects, you can add them to your document at design-time, from within Visual Studio, in the same manner you would add the native object. For example, you can add a Word Bookmark control to a document by opening the Insert menu and selecting Bookmark. Visual Studio discovers your host controls and they appear as extended Office objects that expose properties, methods, and events in your programming model — whether you add them to your document from within Visual Studio, or through the Office user interface, or they were part of your document before opening it in Visual Studio 2005 Tools for Office.
You can drag host controls from the Toolbox onto your documents and worksheets. Visual Studio Tools 2005 for Office adds the most common host controls to the Visual Studio Toolbox. Excel host controls are available in the Excel Controls tab in Excel projects, as shown in Figure 8, and Word host controls are available in the Word Controls tab in Word projects, as shown in Figure 9.
Figure 8. Excel host controls
Figure 9. Word host controls
You can drag host controls from the Data Sources window onto your documents and worksheets. This enables you to add controls that are already bound to data in your documents.
You can dynamically create Bookmark, ListObject, NamedRange and Chart host controls at run time.
Smart Tags Overview
Visual Studio 2005 Tools for Office provides an easy way for you to add smart tags to documents and workbooks. Smart tags are strings of text that have type information attached to them; when a text string that matches the criteria of a smart tag appears in a document, it is recognized and the user is able to perform actions appropriate for that type of string. For example, you could create a smart tag that recognizes stock symbols. When the user types an uppercase string of four letters, a list of stock-related actions, such as looking up a stock price on the Internet, could appear.
Visual Studio 2005 Tools for Office smart tags are designed to be easy and efficient to program in the Visual Studio environment. When you write code for smart tags, you get IntelliSense, integrated Help, support for regular expressions, and the other features of Visual Studio. All of the code for the smart tags is in one class; you do not have to create a separate actions class and recognizer class.
Visual Studio 2005 Tools for Office smart tags are scoped to the document that is part of a specific solution. This means that you can recognize text that might be too general to recognize if the smart tag is applied to multiple kinds of documents; for example, a five-digit number might only represent a product number in an invoice, but it might represent an employee number in a human resources document. If the smart tag only works in the invoice, you do not need to worry that it will incorrectly recognize and assign actions to an employee number.
For more information see Smart Tags Overview.
Actions Pane Support
Microsoft Office 2003 Editions introduced the smart document programmability feature, which allows you to create custom Document Actions task panes that are attached to Word documents and Excel workbooks. The task pane gives users context-sensitive help and functionality while working with a document. Before Visual Studio 2005 Tools for Office, you could program the task pane using the ISmartDocument interface. Visual Studio 2005 Tools for Office provides the ActionsPane object, a managed implementation of the Document Actions task pane, which makes creating custom task panes for Word and Excel solutions much easier.
Figure 10. Actions pane
The actions pane acts as a container of any Windows Forms controls, including user controls that you create. Typically, you add Windows Forms controls to a user control, and then add the user control to the actions pane. Although the ActionsPane object is always available to your solution, there is no visual representation until you explicitly populate it with controls. Once the actions pane is displayed, you can dynamically add or remove controls in response to the user's actions.
You can add multiple user controls to the actions pane and then write code to respond to events on the document that shows and hides the user controls. You can also use the programmable events on the actions pane controls to respond to user interactions with the controls.
For more information, see Actions Pane Overview.
A primary goal of solutions based on Visual Studio 2005 Tools for Office is to separate data from view in Office documents. Data refers to the information that is stored in the document: numbers, text, and so on. View refers to the user interface of the application (Word or Excel). A worksheet, as an example, is a collection of cells with properties like Value (which represents data) and Font (which represents the view). The Excel object model provides access to the various properties and methods of cells (as well as ranges, worksheets, and other elements). Word 2003 and Excel 2003 introduced the ability to map XML schema to various cells and ranges as one step toward separating data from the document view, but Visual Studio 2005 Tools for Office goes further by enabling programmability using native Microsoft .NET types and extending additional application features.
For more information, see Data in Office Solutions.
Data Programming Model
Visual Studio 2005 Tools for Office separates the data from the view in Word and Excel documents by enabling you to embed data as an XML data island. You can then use typed data sets to provide a schema-oriented programming model for interacting with the data island. You can use data binding between the data island and the view to keep the two synchronized.
Programming directly to data by using an XML schema–based model means that code that works with data does not need to address the Word and Excel object models. Code does not rely as much on referring to specific cells, ranges, and tables, which users can move around. Rather, code directly accesses XML data islands.
Data binding works the same way in projects in Visual Studio 2005 Tools for Office as it does in Windows Forms projects. Just as with Windows Forms, you can use the new Data Sources window in your Visual Studio 2005 Tools for Office project to drag and drop a data source onto your document and automatically include a control that is bound to the data source. For example, if you drag the houses table from the WoodgroveBankDataSet data source onto an Excel worksheet, Visual Studio 2005 Tools for Office creates the DataSet, TableAdapter, and BindingSource objects as well as the ListObject control, as shown in Figure 11.
Figure 11. Data objects created by Visual Studio 2005 Tools for Office
Host controls enable either simple or complex data binding.
Simple Data Binding
Simple data binding exists when a control property is bound to a single data element, such as a value in a dataset table. For example, the NamedRange control has a Value2 property that you can bind to a field in a dataset. When the field in the dataset changes, the value in the named range automatically changes to reflect the new data. All host controls, except for the XMLNodes control, support simple data binding. The XMLNodes control is a collection, and therefore it does not support data binding.
Complex Data Binding
Complex data binding exists when a control property is bound to more than one data element, such as multiple columns in a dataset table. For example, the ListObject control has a DataSource property that you can bind to multiple columns in a dataset table. All of the data in the dataset table displays in the ListObject control, and as the data in the data table changes, the ListObject also changes. The only host control that supports complex data binding is the Excel ListObject control. There are other controls, for example the Windows Forms DataGridView control, that also support complex data binding.
Storing the data in a data island provides a mechanism for enabling offline scenarios. When a user first opens a document or requests the document from the server, the data island fills with the most recent data. The data island is cached in the document and is then available offline. The user (and your code) can manipulate the data, even though no live connection is available. When the user reconnects, you can propagate the changes to the data back to a server data source.
To cache a data instance in a document, mark it with the CachedAttribute attribute. Server-side code can manipulate any data instances marked with the CachedAttribute attribute.
Controls that are bound to cached data instances are synchronized with the view, so that any server-side changes that are made to the data show up automatically when the document is opened on the client.
For more information, see Caching Data.
Since the data can now be separated from the view in Word and Excel solutions, it is possible to manipulate the data in a document without starting Office using the ServerDocument class. This is different than the current model, where in order for code to manipulate the contents of the document, Word or Excel has to be running. Having to run Word or Excel blocks a common solution requirement, that is, creating Office documents from an ASP.NET application. Office applications are intended for use in client environments; they are not designed to be run on a server.
You can start the Visual Studio 2005 Tools for Office runtime on a server without running Word or Excel. You can manipulate the data island in a document from any server-side code. When the user opens the associated Office document, the view synchronizes to the data island by using data binding, and the user is presented with the updated data.
In this model, you do not need Word and Excel to write to the data on the server, only to view it on the client. You do not even need Word and Excel installed on the server. This provides improved scalability and the ability to perform fast-batch processing of documents containing data islands on the server.
Your Office document functions as a repository for data, because programmatic access to the data embedded in the document is enabled. Code on the server (for example, in an ASP.NET page) can customize the data in the document and serve up the customized document. When the user opens the document, data binding code in the assembly binds the customized data into the view.
For more information, see Server-Side Access to Data in Documents Overview.
You can use configuration files (config files) to configure settings that are specific to your Visual Studio 2005 Tools for Office solution. You can specify settings such as assembly binding policy, remoting objects, debug, and trace settings.
To add a configuration file to your Office project
On the Project menu, click Add New Item.
Select Application Configuration File in the Templates pane.
In the Name box, type in the same name as the assembly plus the extension .config. For example, a configuration file for an assembly called ExpenseReport.dll would be named ExpenseReport.dll.config.
Deploying a basic Visual Studio 2005 Tools for Office Word or Excel solution generally means working with four files:
The Word or Excel file (template, document, or workbook) that the end user works in
The assembly that contains your compiled custom code and any dependent assemblies such as resources, satellites, or helper libraries
The deployment manifest
The application manifest
To deploy your Visual Studio 2005 Tools for Office solution, copy the document and assembly to the deployment location and create an application manifest and a deployment manifest. Visual Studio Tools for Office includes the Publish Wizard, which copies the files and creates the application and deployment manifests for you. However, you can also do these steps manually.
For more information, see Deploying Office Solutions.
InfoPath Development with Managed Code
Visual Studio 2005 Tools for Office includes support for adding business logic to InfoPath forms using managed code. Managed code allows you to use the .NET Framework CLR to write code in C# or Visual Basic, and to make calls into the .NET Framework class library for advanced tasks such as digital signature processing and encryption/decryption. Visual Studio integration includes F1 Help support, full-featured debugging, and IntelliSense in the code editor. IntelliSense provides statement completion, prompting with lists of members, and inline parameter information on the InfoPath object model.
Visual Studio 2005 includes a new project type: an InfoPath project. The new project type is available under the Office tab in the New Project dialog box, in both Visual C# and Visual Basic, as previously shown in Figure 1.
InfoPath can host Windows Forms controls by wrapping them in an ActiveX control. For more information, see Creating an InfoPath Custom Control in the InfoPath team blog.
InfoPath has a built-in data/view separation, so its object model is entirely data-centric. All data is in an XML form and is bound to the form controls during design time. Developers primarily write code that interacts with the XML behind the form and with the .NET Framework class library.
InfoPath solutions with managed code are deployed in the same way as form templates without managed code. The custom assemblies are included in the form template file itself, so no additional deployment steps are necessary. Specifically, forms can be published to a Microsoft Windows SharePoint Services document library, saved on a file share, installed locally, or even sent as an e-mail message if they are digitally signed.
The managed code inside an InfoPath form template can be affected by several security settings. When an InfoPath form template is set to "Restricted", managed code does not run. When it is set to "Domain", managed code runs, but does not have cross-domain or local disk privileges. When it is set to "Full Trust", managed code also gets full trust. The form template can achieve Full Trust if it has been installed locally, or if it has been digitally signed. The managed code inside an InfoPath form template can also be granted Full Trust through.NET security settings.
Outlook Managed Add-Ins
Visual Studio 2005 Tools for Office includes support for building application-level add-ins for Outlook using managed code. This includes both design-time and run-time support:
Visual Studio project templates help you build Outlook add-ins and add-in setup projects. The support for Outlook is different than for Word and Excel: Outlook does not become a designer inside of Visual Studio like Word and Excel; there are no Outlook host controls, and when you create a Visual Studio 2005 Tools for Office Outlook add-in, the primary class represents the Outlook application because the add-in operates at the application level rather than at the document level.
A dedicated add-in loader component (AddinLoader) creates a separate AppDomain for each add-in, enforces strict security policy, and unloads the add-in when it is disconnected.
The AddinLoader component uses the Visual Studio 2005 Tools for Office run-time engine to provide a single, consistent mechanism for loading both document-level customizations and application-level add-ins into Office applications. For the first time, you can build strongly typed, managed add-ins that you can easily deploy and run under High Security settings.
Microsoft Visual Studio 2005 includes a new project type: an Outlook Add-In project. The new project type is available under the Office tab in the New Project dialog box, in both Visual C# and Visual Basic, as shown in Figure 1 above.
When you select the Outlook Add-in option, Visual Studio creates two projects in the solution:
The add-in project contains starter code for the add-in, and it contains the application manifest.
You use the setup project after you complete development of the add-in and you want to deploy it. The setup project builds a Microsoft Installer (MSI) file, which inserts entries into the registry and deploys the solution files (the assembly and manifests).
Figure 12. Outlook add-in solution
Visual Studio 2005 Tools for Office
Outlook Add-in Tools