Retargeting a Word 2007 Add-In that uses Smart Tags from .NET 3.5 to .NET 4.0
Summary: Given a Word 2007 add-in including one or more smart tags targeting the Microsoft .NET Framework 3.5, you may want to change the add-in to target .NET Framework 4.0 so that you can take advantage of new framework features. Changing the target version of the .NET Framework for a Word add-in from .NET Framework 3.5 to .NET Framework 4.0 is easy, but requires specific steps.
Applies to: 2007 Microsoft Office system | Microsoft Office Word 2007 | Visual Studio 2010
Published: August 2010
Provided by: Ken Getz, MVP, MCW Technologies, LLC
Scenario: You created an add-in for Microsoft Office Word 2007 that includes a custom smart tag and a Ribbon customization. You built it using Microsoft Visual Studio 2008 for the 2007 Microsoft Office system and the Microsoft .NET Framework 3.5. Now, given Microsoft Visual Studio 2010 and the .NET Framework 2010, you want to update your add-in to target the new version of the Microsoft .NET Framework. It's easy to modify the target version of .NET, but you find that doing so has caused your add-in not to compile. You need to modify the code slightly to use the new interface-based features in the Office Extensions for the .NET Framework 4.0. The sample that is included with this article includes a working Word 2007 add-in, created by using Visual Studio 2008. This add-in targets the .NET Framework 3.5, and it includes two features. The add-in provides a custom smart tag which recognizes temperatures in Fahrenheit or Celsius, and offers the option to convert to the other scale, shown in Figure 1. The Ribbon customization, shown in Figure 2, lets you enter a temperature value, set its scale, and then insert the temperature into the document. You may find the internal code for the add-in to be of interest, but it's not the focus of this article. Instead, this article focuses on migrating this add-in so that it targets the .NET Framework 4.0. Targeting the .NET Framework 4.0 lets you take advantage of the fact that the Office Extensions for the .NET Framework 4.0 use a runtime that is version-resilient, based on interfaces instead of classes. Combined with type embedding, this makes your solution much less dependent on the version of Office installed on end user computers. For more information about the benefits of upgrading your add-in so that it targets the .NET Framework 4.0, see the links in the Explore It section.
Follow these steps to convert the add-in so that it targets the .NET Framework 4.0. To Set the Target Version
To Fix the SmartTag
In projects that target the .NET Framework 3.5, code in the add-in class calls the GetVstoObject method of the host document, retrieving the extended object that adds managed functionality to the host document. In projects that target the .NET Framework 4.0, code must instead call the Globals.Factory.GetVstoObject method, passing in a reference to the native Word or Excel object that you want to extend. Therefore, follow these steps to fix up the call to GetVstoObject. To Fix up ThisAddIn
To Fix up the Ribbon Customization
If you have followed the steps carefully, you should now be able to compile and run the modified version of the sample Word add-in. It's important to find any code that instantiates Ribbon controls outside the designer class, and call the appropriate factory method to create the instance instead. In the Ribbon class itself, you can simply call Factory.Create<ControlType>. In other classes, you can use the Globals.Factory instance, calling the Create<ControlType> method. There are no instances of this need in the sample project, but there may be in your own projects. The example you have seen here includes many of the issues that you will encounter when updating your own add-ins to target the .NET Framework 4.0, but clearly, you will have other issues as well. Review the documents listed in the Explore It section--you will find complete documentation on the kinds of issues you're likely to encounter, as well as full documentation on the new factory methods and other important information. You may find it simpler to start from scratch. That is, it may be easier to re-create the user interface for your Ribbon customizations, allowing the Visual Studio 2010 designer to create the appropriate code for you. Then, you can copy over your own code, and fix up any minor issues that you find. For a large and complex customization, this may be a simpler solution. |
Video Length: 08:59 File Size: 12.8 MB WMV
About the Author Ken Getz (MVP) is a developer, writer, and trainer, working as a senior consultant with MCW Technologies, LLC. In addition to writing hundreds of technical articles over the past fifteen years, he is lead courseware author for AppDev (http://www.appdev.com). Ken has co-authored several technical books for developers, including the best-selling ASP.NET Developer's Jumpstart, Access Developer's Handbook series, and VBA Developer's Handbook series. Ken is a member of the INETA Speakers Bureau, and speaks regularly at a large number of industry events, including 1105 Media's VSLive, and Microsoft's Tech-Ed. |