views:

111

answers:

2

I'm having some trouble with a word add-in. I'm attempting to create a new document format, and have it sort-of working. The format is just an encoding on top of an existing docx. In other words, I can strip off the encoding and be left with a valid word document.

Saving:

Right now, when the user wants to save a document in the new format, I prompt to save the original, and then read the original from disk, saving a copy in the new format. This leaves 2 files on the filesystem (foo.docx and foo.myformat).

Ideally though, I'd like to intercept the document in the DocumentBeforeSave event, get the document as a byte array, transform it in memory, and just save a single copy. I can't seem to find a way to get the byte array however.

Opening:

Right now when the user selects a file to open that is stored in my format, I make a temporary copy (tmp\blah.docx), and then just open the document with Application.Documents.Open(tmp\blah.docx...).

I'd prefer instead to create the document from a memory stream, so I can avoid creating temporary files. Is it possible to create a new Word.Document from memory?

Any direction would be greatly appreciated. Thanks much.

+1  A: 

What you are trying to do looks like a classic import/export converter for Word documents to me. From Word 2007 SP2 on, you can use an OpenXML based API to create your converter.

This MSDN article describes how to do it:

Introducing the Open XML Format External File Converter for 2007 Microsoft Office System SP2

There is also a sample project available on MSDN:

Open XML Converter Interface Sample Code

0xA3
A: 

You can get the document in VSTO or VBA using .WordOpenXML

plutext