Custom file format for Word?

Add-in Express™ Support Service
That's what is more important than anything else

Custom file format for Word?
How would this be done? 
Andrei B




Posts: 89
Joined: 2009-09-30
Hi,

We need to create a custom document format that would be loaded into word. The file is basically a ZIP archive with 2 files: First is the actual text data file (could be .txt file or .doc or .docx file) and the second is a plain text file that has meta-data info in it.

Using Add-In-Express, how would I go about recognizing this format in Word when it opens a file, and extracting the files somewhere and then have Word open the content file?

Then when saving we would have to archive the file back into the custom file format.

How is this done? Where do I start? Thanks!!
Posted 06 Nov, 2009 16:23:22 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Andrei,

Using Add-In-Express, how would I go about recognizing this format in Word when it opens a file, and extracting the files somewhere and then have Word open the content file?


Unfortunately Word doesn't allow to extend a number of file extensions which it uses.
.NET, Extended MAPI, Windows Phone & RT
Posted 07 Nov, 2009 14:22:38 Top
Andrei B




Posts: 89
Joined: 2009-09-30
Are you sure this isn't possible?

Because if open a .Zip file in Word it still calls the OnDocumentLoad event handler, so there must be a way to override the default behavior and handle the custom file format.
Posted 07 Nov, 2009 23:12:42 Top
Andrei Smolin


Add-in Express team


Posts: 16162
Joined: 2006-05-11
Andrei,

This isn't possible. Absolutely.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Nov, 2009 09:45:58 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hello Andrei,

Sorry, I was wrong. I have created a sample add-in for testing purposes. Here I pasted a small piece of code illustrating the task:

Microsoft.Office.Core.FileDialog fd = WordApp.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogOpen);
Microsoft.Office.Core.FileDialogFilters filters = fd.Filters;
filters.Add("Eugene Astafiev", "*.zip", 1);
if (fd.Show() == -1)
{
    // here you can load the document into Word
    System.Windows.Forms.MessageBox.Show(fd.SelectedItems.Item(1));
}


Please note that I used version specific PIAs.

Using Add-In-Express, how would I go about recognizing this format in Word when it opens a file, and extracting the files somewhere and then have Word open the content file?


You can use the System.IO.Compression namespace to unzip files from archive. That article will be useful. After you unzipped files from package you can open them in Word.
.NET, Extended MAPI, Windows Phone & RT
Posted 10 Nov, 2009 09:57:04 Top
Andrei B




Posts: 89
Joined: 2009-09-30
Actually, my developer Andrew decided that were not going to pursue a custom file format any more. Instead we'll go another way.

However I was able to hack a custom file format.

When Word opens a document, an OnOpenDocument event handler gets called. Into this handler, I added code that extracted the file extension from the document name. If the file extension was for our own custom file format, I would close the document, unzip the files from the file that was just opened and closed, and then open the correct file that was from the unziped archive.

This worked but it was ugly because Word would show a bunch of garbage when it first opened the zip file, but then it would go away right away and the right document would get loaded.

Anyway, thanks for all the help!
Posted 10 Nov, 2009 14:03:16 Top