Building VSTO Without Office Installed

I’m not a big believer in testing on the same machine you build on – it’s a good way to inadvertently design bugs into your code that only appear when your code is deployed. So I keep a build machine, where I do all my coding/building, and I run my projects on other machines – usually a VM. Usually, this does not cause me a problem. Yesterday, while working on a Form Regions issue, I encountered a dependency the VSTO folks built into their build process, and wanted to share the workaround.

We begin by creating a C# Outlook 2007 Add-In project in Visual Studio 2008, which builds just fine. Since Outlook isn’t installed, we use the Publish wizard to publish the add-in to a share, and from there we can install it on our test machine. So far so good. Then we add an Outlook Form Region. Before we do any customization on it, we test build, and get this:

 ------ Build started: Project: MyFormRegion, Configuration: Debug Any CPU ------ 
<very long build command cut for clarity>

Compile complete -- 0 errors, 0 warnings
MyFormRegion -> C:\projects\MyFormRegion\bin\Debug\MyFormRegion.dll
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v9.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.Office2007.targets(154,9): error :
   Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Done building project "MyFormRegion.csproj" -- FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

This much reviled error message gets a lot of traffic on the interwebs, but there’s not much helpful information about what it means. That is, until I include the term “Form region” in my search. That cuts the chatter to one hit, a discussion between a developer who uses an environment similar to mine, and Daniel Molina, one of our VSTO developers. Between the two of them, they figured out the failure occurs when we try to use reflection during the build process. They figured out that installing the Outlook PIA into the GAC would fix the problem. I tried it here:

 C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\Bin>gacutil.exe /i "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Vis
ual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Outlook.dll"
Microsoft (R) .NET Global Assembly Cache Utility.  Version 2.0.50727.42
Copyright (c) Microsoft Corporation.  All rights reserved.

Assembly successfully added to the cache

And now I can build and publish my Form Regions sample. Note that in their discussion, they mention installing the PIA on the build development machine. As you can see from the path above, I did not have to do this as Visual Studio 2008 actually ships the PIA. So installing it in the GAC was sufficient. Now I can get back to the debugging I originally set out to do.