Sending Office Documents with VSTO Customisation to Other Parties

When you create a document-level customisation for Office using VSTO, the customisation is compiled into a .NET assembly and the location of this assembly is stored in a custom document property called _AssemblyLocation. When loading the document, Office first checks to see whether this custom property is present and if it is, it tries to load the customisation. Now what if you are going to send this document to another organisation or department who don't have (or don't want to have) that customisation (assembly)? Since they don't have the assembly on their machine, they will see an error message presented via a dialog box saying that the assembly could not be found, which is not nice.

One way to prevent the code from running but without receiving an error message is to hold down the SHIFT key (as described on MSDN) when you are opening the document via the File menu. Note that this solution does not work if you are opening the document using the "Getting Started" task pane. This solution works but it requires the user to open the document using the File menu and they need to remember to hold down the SHIFT key so it is not necessarily a desired solution.

The alternative solution is to remove the custom document property that points to the customisation assembly. This way Office will not try loading the customisation assembly on the end-user's machine so they will not get the error message. However, you may need to put those custom properties back in the document if they user modifies the document and returns the document back to you.

So how can you remove the custom properties?

Manually
Open the document in the Office application (Word, Excel or PowerPoint), go to Properties -> Advanced Properties -> Custom and remove the assembly location custom property.

ServerDocument Class in VSTO
Both versions of VSTO (VSTO 2005 SE and VSTO 3.0) come with their own version of this class and have a very similar functionality. Note that you don't need to have Office installed on the machine that is trying to modify the document using the ServerDocument class so you can perform this process on a server machine as part of an automated process that you run before sending out Office documents. You can find more information on this approach here.

OpenXML
If you are using Office 2007, you can access and manipulate the custom document properties by using the types in the System.IO.Packaging namespace or by using the Open XML Format SDK. Again, you don't need to have Office installed on the machine that is using these components to manipulate the Office documents.

As I mentioned earlier, when you receive the document from the other party, you may want to add the VSTO customisation again. You can use any of the approaches mentioned above to put the custom properties back on.

(Thanks to Mary Lee for the pointers)

Originally posted by Mehran Nikoo on December 18th 2008 here.