views:

472

answers:

2

I want to add the option to export to a new file format in Word 2007. Ideally it would be nice if the option could be another file format in the Word 2007 Save As dialog that the user could select in the file format dropdown box.

Although I have a lot of .NET experience I haven't done much development for MS Office. At a high level what should I look at to add another save as format to Word 2007 using .NET?

+2  A: 

You basically have two options in Word 2007 to add your own custom file export filters:

0xA3
+1  A: 

Take a look at the Microsoft.Office.Core.FileDialog interface and its Filters property (which is type Microsoft.Office.Core.FileDialogFilters), where you can add and remove filters. They are included with Visual Studio Tools for Office 12, in Office.dll.

As to getting the correct FileDialog object, first acquire a Microsoft.Office.Interop.Word.Application instance (usually by creating a new ApplicationClass or, equivalently, using VBA's CreateObject) and call it application. Then do something like the following:

Microsoft.Office.Core.FileDialog dialog = application.get_FileDialog( Microsoft.Office.Core.MsoFileDialogType.msoFileDialogSaveAs );
dialog.Title = "Your Save As Title";
// Set any other properties
dialog.Filters.Add( /* You Filter Here */ );

// Show the dialog with your format filter
if( dialog.Show() != 0 && fileDialog.SelectedItems.Count > 0 )
{
    // Either call application.SaveAs( ... ) or use your own saving code.
}

The actual code can either be in a COM Add-In, or an external program that uses COM to open/interact with Word. As to replacing the built-in Save As dialog, you'll also need to handle the Microsoft.Office.Interop.Word.Application.DocumentBeforeSave event somewhere (VBA, with this code, etc) to intercept the default behavior.

Here's an example 'save as' handler:

private void application_DocumentBeforeSave( Microsoft.Office.Interop.Word.Document document, ref bool saveAsUI, ref bool cancel )
    {
        // Be sure we are only handling our document
        if( document != myDocument )
            return;

        // Allow regular "Save" behavior, when not showing the "Save As" dialog
        if( !saveAsUI )
            return;

        // Do not allow the default UI behavior; cancel the save and use our own method
        saveAsUI = false;
        cancel = true;

        // Call our own "Save As" method on the document for custom UI
        MySaveAsMethod( document );
    }
Arc
have you tested this? I didn't think msoFileDialogSaveAs could have filters added to it.
Otaku
Ah you're right. I've added file extensions to the msoFileDialogOpen before, but Word does not allow new extensions to the Save As dialog.The rest of the code works, but you'll need to use a different "Save As" dialog for the actual behavioral extension, such as `System.Windows.Forms.SaveFileDialog`.
Arc