The Visual Studio 2005 Release Candidate is out, with final launch approaching fast. This is great news for everyone, with developers of all stripes getting the love with this release. And sometimes-overlooked Office developers are no exception. Visual Studio Tools for Office 2005 (VSTO 2005) gives Office developers unprecedented power for creating Office applications, including all that Visual Studio has to offer.
If you're an MSDN subscriber, you can download VS 2005 now. Otherwise, you may have to wait a short while longer. But whatever the case, Office developers should know about a few exciting new features. I hinted about some of these last spring. Now is a good time to dive a little deeper and see how they work.
Must Know #1: Separation of Data from Document
In probably its biggest innovation for Office app developers, VSTO 2005 separates your application's data from its view. Application content, i.e. text, numbers, etc., is stored in XML data islands that are associated with your Word or Excel document through data binding. So instead of storing hard data, the document need only store the XML schema mapping the data to its proper place within the document.
This opens up a lot of doors for working with your application data. At this point, you (or your users) don't need to start Word or Excel to affect the data. You can code directly to the data using a schema-oriented programming model driven by typed data sets. You don't even have to work directly with the Document Object Models, as robust as they have become.
Server Side Programming
Since the data islands can be manipulated without starting Word or Excel, it makes sense that you can also create new data islands, and thus new documents, without starting Word or Excel. In fact, with VSTO 2005 you can create new documents on the server, say through an ASP.NET app, on the fly. You don't need to open Word or Excel to write the data, in fact you don't even need them installed on the server. You just need them on the client. When your user opens up the document, data binding dictates how the data appears.
Distributed Data Applications
Use of XML data islands also presents some interesting scenarios for distributed applications with a centralized data source. Suppose your user opens a local Excel app. That app snags the most recent data from a central server and downloads a cached copy of it. The user goes offline, does his thing, makes some changes, whatever. Next time he starts up in a connected environment, the Excel app can load the changes back to the central server.
Must Know #2: Smart Tags
Maybe you've seen Smart Tags in action. You're going along, typing something pretty standard, like an address. Word recognizes that you've just typed an address and underlines it in red, with a little floating dropdown button. Clicking the dropdown, you see all sorts of cool options, like finding a map to that address or getting driving directions.
Smart Tags can be powerful little tools and mightily helpful to your user. Unfortunately, in the past they've also been mightily tricky to build. Not anymore. One of the hot items in VSTO 2005 is the evolution of Smart Tags.
Here are the key components for building Smart Tags in VSTO 2005:
- A new reference: Microsoft Smart Tags 2.0 Type Library
- A new class: Microsoft.Office.Tools.Word.SmartTag
- The Action class: Microsoft.Office.Tools.Word.Action
- The Expressions property of the SmartTag object
- The Actions property of the SmartTag object
- The VstoSmartTags property of the document object
The new and improved Smart Tags also have a few other advantages:
- All the code can be kept in a single class. In other words, no separate action and recognizer classes.
- They can access the Actions Pane (see below) and other objects.
- They have more limited document scopeyou can make the expression that triggers the Smart Tag specific to the local document rather than globally.
Those are the key components you should know abouthow they all work together will be demonstrated in the code sample below.
Must Know #3: Actions Pane
But before I get into the
code sample, I want to mention one more extremely cool feature that's been beefed up in VSTO 2005: the Actions Pane. Office users should be familiar with task panes, such as the Style and Formatting pane in Word or the XML Source pane in Excel. An actions pane is very similar and is, in fact, hosted within the Office task pane. With it, you can add your own complex tasks that users can kick off with a click.
Now a Windows Forms control, the Actions Pane object can be added to your VSTO app as easily as any other control. It also serves as a container for other controls. And with VSTO 2005, you no longer need the XML Expansion Pack or XML Mapping.
Using actions panes gives you a couple of big advantages over form controls:
- Controls in actions panes will not be printed and do not need to be hidden (which isn't possible in a Word doc, anyway).
- The controls also don't need to be placed on the document itself, which can sometimes just get in the way.
Sample Code: Boxes!
Now let's do a quick demo of Smarts Tags and an Actions Pane. To do that, you're going to build the ultimate killer app, something everybody needs, though nobody realizes it: ASCII boxes! You're going to create a Word document that lets you build an ASCII grid on the fly in not one, but two, different ways. The boxes don't really do anything, but think of them as building blocks for something wildly imaginative. The source code for this article is available for download
here.
Since this is a code sample and not a walkthrough, you might want to check out line-by-line walkthroughs on MSDN for "Creating a Basic Actions Pane in Word" and "Creating a Smart Tag that Converts Temperatures from Fahrenheit to Celsius."
1. To start, create a new Word project called "VSTO2005 Demo". Call your document "Boxes".

Figure 1. Creating a New Word Project in VSTO
2. In the Solutions Explorer, right-click ThisDocument.vb under the Boxes document and select "View Code".

Figure 2. Displaying the Document's Code Window
3. Add a new project reference for "Microsoft Smart Tags 2.0 Type Library" (Project -> Add Reference -> COM tab).

Figure 3. Adding a Reference to the Smart Tag Library
4. Highlight all the code (Ctrl-A) and replace with the following code. Most of this pertains to the Smart Tags, except for Me.ActionsPane.Controls.Add(addBox).
Imports System.Text.RegularExpressions
Public Class ThisDocument
WithEvents boxAction As Microsoft.Office.Tools.Word.Action
Dim addBox As New AddBoxes
Private Sub ThisDocument_Startup(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup
Me.ActionsPane.Controls.Add(addBox)
Dim boxTag As New Microsoft.Office.Tools.Word.SmartTag( _
"http://www.vsto2005demo.com#smarttag", _
"Boxes")
boxTag.Expressions.Add(New _
Regex("(?<rows>\d{1})x(?<cols>\d{1})"))
boxAction = _
New Microsoft.Office.Tools.Word.Action("Make Boxes")
boxTag.Actions = _
New Microsoft.Office.Tools.Word.Action() {boxAction}
Me.VstoSmartTags.Add(boxTag)
End Sub
Sub boxAction_Click(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) _
Handles boxAction.Click
Dim r As Integer = Convert.ToInt16(e.Properties.Read("rows"))
Dim c As Integer = Convert.ToInt16(e.Properties.Read("cols"))
Dim g As New Grid(r, c)
e.Range.Text = g.drawBoxes
End Sub
Private Sub ThisDocument_Shutdown(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub
End Class
5. Add a new Actions Pane control. Right-click the "VSTO2005 Demo" project and select Add -> New Item -> Actions Pane Control. Name it "AddBoxes.vb".

Figure 4. Adding a New Actions Pane Control
6. If the Toolbox is not visible, make it appear by clicking on it in the toolbar, selecting View -> Toolbox, or pressing Ctrl-Alt-X. Drag two Labels, two NumericUpDowns and a Button from Common Controls to the AddBoxes design view so that they look like this:

Figure 5. The Add Boxes Control in Action
Change the value of the first label to "Rows" and the second label to "Columns". Then change the properties of the other controls thusly:
First NumericUpDown
name: getRows
value: 3
maximum: 9
minimum: 1
First NumericUpDown
name: getCols
value: 3
maximum: 9
minimum: 1
Button
name: addBox
text: Add Box
7. Double-click the new addBox button or right-click AddBoxes.vb in the Solution Explorer and choose "View Code". Replace what you see with the following:
Public Class AddBoxes
Private Sub addBox_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles addBox.Click
Dim g = New Grid(getRows.Value, getCols.Value)
Globals.ThisDocument.Range(0, 0).InsertAfter(g.drawboxes)
End Sub
End Class
8. Right-click the "VSTO2005 Demo" project and choose Add -> Class. Name the new class Grid.

Figure 6. Adding a New Class
9. Replace the contents of this new class with the following code. This will be used by both the Smart Tag and the Actions Pane.
Public Class Grid
Private numRows As Integer
Private numCols As Integer
Public Property Rows() As Integer
Get
Return numRows
End Get
Set(ByVal value As Integer)
numRows = value
End Set
End Property
Public Property Cols() As Integer
Get
Return numCols
End Get
Set(ByVal value As Integer)
numCols = value
End Set
End Property
Public Function drawBoxes() As String
Dim box As String = "|_"
Dim top As String = "_"
Dim endcap As String = "|"
Dim c As Integer
Dim r As Integer
Dim thisGrid As String = " "
For c = 1 To numCols
thisGrid += top + " "
Next
thisGrid += vbCrLf
For r = 1 To numRows
For c = 1 To numCols
thisGrid += box
Next
thisGrid += endcap + vbCrLf
Next
drawBoxes = thisGrid
End Function
Public Sub New(ByVal r As Integer, ByVal c As Integer)
numRows = r
numCols = c
End Sub
Public Sub New()
numRows = 3
numCols = 3
End Sub
End Class
You should be ready to save and run. Hit F5 to test the new app. You should get a new Word document named "Boxes". You'll see your new Actions Pane on the right. Try setting the number of rows and columns and adding boxes. They should be inserted at the start of your document in whatever font you're using (though they'll look better if you change it to Courier).
Now type some stuff. When you type something that matches the pattern you specified for the Smart Tag, you should get a red underline and helpful pop-up options. In this case, the pattern you created will watch for a single digit, followed by "x", followed by another single digit, such as "4x5". When your app sees something like this, it'll give the user a chance to replace it with helpful, playful, oh-so-versatile boxes. Like this:
_ _ _ _ _
|_|_|_|_|_|
|_|_|_|_|_|
|_|_|_|_|_|
|_|_|_|_|_|
Figure 7. An Example ASCII Grid
Must Know #4: Object Test Bench
As you explore your new coding options, you have a massive set of tools with which to test and debug your burgeoning app. But if you are building objects in your Office app, then be sure to check out one new tool in particular: the Object Test Bench (OTB). This tool lets you instantiate your class and test its properties without you having to build a separate test harness.
To use it, find the object you want to test in Class View and right-click it. You should see an option to create an instance of it. Be aware, though, that this tool only works for certain scenarios. If you don't see the option, it could be because your object doesn't meet one of the OTB's requirements:
- The OTB will show all functions, methods, constructors, and subroutines of the object you're testing, whether it's private or public (or some other access modifier). However, they can't be operators, destructors, or "Generic".
- Return types and parameters can't be "Generic" or mathematical expressions.
- Return types cannot be variables.
- Value types cannot be primitives.
Must Know #5: Deployment Requirements
One last thing. Now that you've built your beautiful new Office app, you need to prepare the end user's computer to run it. Make sure your users meet the following requirements:
- .NET Framework 2.0 is installed.
- They have a version of Office 2003 installed that supports VSTO 2005, preferably the latest version, with all the latest patches and updates.
- The primary interop assemblies are installed. You can also supply these in redistributable form. The installer is available from the Microsoft Download Center.
- The Visual Studio Tools for Office runtime is installed.
- You've granted full trust to all assemblies. If this is a Word or Excel application and it's being deployed on a network, you also need to grant full trust to the documents.
For more information on deploying your Office app, see "How to: Deploy Office Solutions."
VS 2005 and VSTO 2005, in particular, have a lot of new features and improvements to old ones that you'll want to know more about. As an Office developer, you'll especially want to investigate changes to the Document Object Model and the whole concept of separating Office data from its view. Also be sure to check out new coding options for Smart Tags and Actions Panes, both of which can add some strong features to your apps, as in this code sample. Today, ASCII boxes, tomorrow, the world.
Resources
What's New in Visual Studio Tools for Office
Smart Tags Overview
Action Panes Overview
Object Test Bench
Deploying Word and Excel Solutions