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)
One of the namespaces introduced in the .NET Framework 3.0 is System.IO.Packaging (in the WindowsBase assembly), which provides classes that support storage of multiple data objects in a single container, like a ZIP file. These data objects can be referenced in a hierarchical format, similar to the file system. This functionality can be used to open, read and manipulating the files based on the Open XML standard since the Open XML file format follows the same principle. A while back, I wrote a piece of code for a customer to use the classes in this namespace for setting the values for custom properties in Word 2007 documents and Excel 2007 spreadsheets. This wasn’t hard but I had to use XPath queries and use the appropriate XML namespaces in order to find the elements I was trying to update.
We then had an initiative to create an SDK that provides strongly-typed part classes for use with the documents based on Open XML. This SDK was available in CTP mode until earlier this week when its first version was released. You can now download the Open XML Format SDK from the Download Center. This SDK relies on the functionality provided by the System.IO.Packaging namespace so it requires .NET Framework 3.0.
The Open XML Format SDK makes the life so much easier for the developers, but wait… we are not finished yet. Right after the release of the Open XML Format SDK, another open source project called “PowerTools for Open XML” was announced, which provides more than 30 PowerShell cmdlets to manipulate the Open XML files. So for example, there are cmdlets for manipulating the watermark, header and footer, style, etc. This means you can read and edit Open XML files on the server-side without using the Office object model and without a need for an Office 2007 license on the server machine. Here is a great screencast that includes some nice demos that show how you can use the PowerTools to go through a list of Word documents, add a watermark to all of them and make sure they all use the same text style. There is also another demo that performs a mail merge using a simple PowerShell script.
I remember that a few years back, I was working on a project where the customer decided to spend a considerable amount of cash on a document generation and rendering solution and the development team had to write a lot of code to populate the templates provided by the third-party solution. The integration and testing experience was quite painful too. It is great to see how the publication of Open XML as a standard and the development of great tools by Microsoft and the open source community has simplified the document generation process and I can clearly see how this will result in reduction of licensing and development costs.
Great news: ISO/IEC DIS 29500 (Office OpenXML) has received the necessary votes for approval as an ISO standard.
This decision was made by the national bodies in the Ballot Resolution Meeting in Geneva during the week 25-29 Feb 2008. In order to be approved as an ISO standard, at least 66% of the votes cast should be positive and no more than 25% of the votes should be negative. By the end of the ballot resolution process (at midnight on Sat 29 Mar 2008), 75% of the votes cast were positive and only 14% of the votes cast were negative so the criteria for approval as an ISO standard have been met.
Microsoft will apply changes to its technology and business practices, which will cover high-volume business products including Windows Vista, Windows Server 2008, SQL Server 2008, Office 2007, Exchange Server 2007, Office SharePoint Server 2007 and all future versions of these products.
Here are some of the specific actions Microsoft is taking to implement these new interoperability principles:
Ensuring open connections to Microsoft’s high-volume products
Documenting how Microsoft supports industry standards and extensions
Enhancing Office 2007 to provide greater flexibility of document formats
Launching the Open Source Interoperability Initiative