HowTo: Store Microsoft Office documents in your custom data storage

One of the tasks that developers often run into is saving and retrieving Office documents (or document copies) from external data storage. This storage can be an SQL database, Web-service, FTP server as well as a shared network resource of a local area network. Let's try to handle this task for such popular MS Office applications as Word, Excel, PowerPoint and Outlook using Visual Studio 2005 VB.NET and C#.

Data storage implementation

Let's start with implementing data storage. In our case it will be a Web-service with three methods:

1. SaveData. This method is used to save the document as a byte array. The first parameter is the type of the document being saved, the second one is the document name and the third is its contents.

2. LoadData. This method is used to load the document as a byte array. The first parameter is the type of the document being inquired, the second is the document name, and the returned result is the contents of the document or null if the inquired document does not exist.

COM add-in implementation

Now let's proceed to implementing the COM add-in for MS Word, Excel, PowerPoint and Outlook. Each Object Model of these host applications contains its own features, that's why the code turned out to be quite bulky and it doesn't make sense to publish it here in full. I will make an outline of the main problems and their solution methods and you can use the links at the end of this post to download the entire code. So, the main problems are:

How to save an active document

Outlook. It's easy, you take Outlook._MailItem from the active inspector, save it into a temporary file using the SaveAs method, read the contents of this file using the System.IO. File.ReadAllBytes method and pass the contents onto our web-service through the SaveData method.

Excel and PowerPoint. It's not that easy in this case. These documents can be edited after they are opened from our data storage. That's why you need to somehow mark them to save the subsequent alteration back to the storage. You can use the CustomDocumentProperties collection to mark the document. Add a string value with a unique name to this collection, save the active document to a temporary file using the SaveAsCopy method, read the contents of this file using the System.IO. File.ReadAllBytes method, pass it on to our web-service with the SaveData method, remove the unique record from the CustomDocumentProperties collection of the active document and save the local document.

Word. It is the most complex case, because the Object Model of this application doesn't have the SaveAsCopy method. So we will have to use the SaveAs method and then reopen the original document that the user worked with before saving. All other operations are the same as in Excel or PowerPoint.

How to open a document from data storage

Irrespective of host application you need to get a local copy of the saved data. You get a list of document names using the GetListOfData method, then suggest the end-user selecting the necessary document, then get its contents as bytes array using the LoadData method and save the received bytes array into a file.

Excel, Word and PowerPoint.Open a workbook, document or presentation using the Open method of the respective host application.

Outlook. Since the Outlook Object Model doesn't have the Open method, you can open the saved msg file using the following VB.NET code:

System.Diagnostics.Process.Start(<PATH_TO_EMAIL_MESSAGE_FILE>)

How to save a document opened from data storage

And a few words about saving a document which was opened from data storage:

Intercept the event of the standard Save button. To do this you'll need two components, AddinExpress.MSO.ADXBuiltInControl with Id = 3 for Word, Excel and PowerPoint 2000 – 2003 and AddinExpress.MSO.ADXRibbonCommand with IdMso = "FileSave" for Word, Excel and PowerPoint 2007.

First we check the CustomDocumentProperties collection in the ADXBuiltInControl.ActionEx and ADXRibbonCommand.OnAction event handler. If the document is marked with the unique record, cancel the standard operation, save the document using the Save method, read the file contents using the System.IO. File.ReadAllBytes method and transfer the received bytes array to our web-service using the SaveData method.

Ah, almost forgot… What do you do with a heap of temporary files that were created when opening documents from data storage? Just delete them. To do this, add a full path to each file that was created to a special list and then remove all the files from this list in the AddinBeginShutdown event handler.

Everything described above is one of several possible options of handling such tasks. Additionally, you can try the following points: disable a standard SaveAs button, abandon custom buttons and process standard Save and Open, process keyboard shortcuts, etc.

This technology is now available for our custom development services only. Based on the Add-in Express for Office core, it is designed for building custom-tailored Office add-ins with far less coding than you usually have to do. Plus, it includes all Add-in Express features such as True RAD, visual designers, Outlook view and form regions, etc.

Get the best platform for building version-neutral, fast and easy deployable plug-ins by using Add-in Express projects templates, visual designers, components and wizards in combination with a perfect Delphi compiler.

This is an extension for Visual Studio that allows developers to quickly create WiX-based setup projects in a familiar Visual Studio way.

The Designer for WiX Toolset lets you forget the plain Windows Installer XML and concentrate on your deployment logic. It integrates several editors with the Visual Studio IDE and provides a set of vdproj designers to configure the file system, registry, user interface, custom actions, launch conditions and more for your setup projects.

The innovative technology for customizing Outlook views and forms. It is included in all Add-in Express for Office products and can be used to extend Outlook views, e-mail, task and appointment windows, To-Do bar, Reading and Navigation panes with your own custom sub-panes.

Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in
the United States and/or other countries. All other trademarks are property of their respective owners.