Article: Office Space – From VBA Macro to Word Add-In

Document automation isn’t a new functionality by any means. It has been going on since the invention of macros, and there has been a full-fledged programming model within Microsoft® Office applications since the early 1990s. For many years, the Office tools have featured macro recorders that weren’t limited to simply replaying keystrokes but have been capable of writing code on the fly as well. Macros have even demonstrated that they are functional enough to write viruses. And while writing a virus using a Word macro may be a dubious feat, it is a feat nonetheless.
However, even with all of the capabilities of Visual Basic® for Applications (VBA), there are some things it just doesn’t do very well. In the early days of VBA, XML hadn’t yet been invented, the Internet was in its infancy, and the first HTML pages were just beginning to appear. As such, it’s no wonder that making a Web service call isn’t well handled in the context of Visual Basic for Applications.
The Microsoft .NET Framework CLR, on the other hand, understands these technologies quite well. The ability to call Web services is just one among a number of reasons for wanting to write .NET-targeted code rather than VBA—and doing so means using Visual Studio® Tools for Office (VSTO). But switching from VBA to VSTO doesn’t have to be a case of throwing the baby out with the bath water; rather, it can be just a natural way to extend how you are already developing solutions in Office.
In this column you’ll see how to use Word to capture some VBA code that solves a basic problem. Then I will use the latest version of VSTO included with Visual Studio 2008 to wrap this code into a deployable Word add-in. I’ll also write some simple code for some of the tasks that the macro recorder either just can’t record or doesn’t record in an optimal way. [Article removed]