Add Save as file extension for Excel

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

Add Save as file extension for Excel
 
nwein




Posts: 529
Joined: 2011-03-28
Is it possible to add my own file extension (e.g. .adx) to the save as dialog in Excel (2010)?
Posted 25 Jul, 2012 13:04:52 Top
Eugene Astafiev


Add-in Express team


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

Please try to modify (add your own file extension) the Filters property of the FileDialog class from the Excel Object Model.

Also please take a look at the Using Word Dialogs programmatically: C# sample article on our technical blog.
.NET, Extended MAPI, Windows Phone & RT
Posted 25 Jul, 2012 15:02:55 Top
nwein




Posts: 529
Joined: 2011-03-28
Thanks for pointing me to the right direction.
I would like to display my modified msoFileDialogSaveAs and msoFileDialogOpen (i.e. with the additional filter) when user clicks the open and/or save as options. Do I need to intercept those events? (either from shortcut or from menu click)

Addendum:
While your sample for the open file dilaog works:

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


The same doesn't work with msoFileDialogSaveAs (throws a Member not found exception).
Any idea how to tackle this?
I understand it's not entirely Add-in express related but still thought I could harness your Excel brains for the task :)
Posted 25 Jul, 2012 17:38:45 Top
Eugene Astafiev


Add-in Express team


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

First of all, you don't release underlying COM objects properly. For example:

filters.Add("Eugene Astafiev", "*.zip", 1); 


The FileDialogFilters.Add method returns an instance of the FileDialogFilter class which should be released after.

MSDN provides the the following description for this issue:

A run-time error will occur if the Filters property is used in conjunction with the Clear, Add, or Delete methods when applied to a Save As FileDiaog object. For example, Application.FileDialog(msoFileDialogSaveAs).Filters.Clear will result in a run-time error.


It looks like the Save As dialog can't be customized in Office.
.NET, Extended MAPI, Windows Phone & RT
Posted 26 Jul, 2012 11:37:21 Top
nwein




Posts: 529
Joined: 2011-03-28
I saw that MSDN as well, was hoping there's another way of doing it....
That leads me to ask - can I provide my own save as dialog, mimicking Excel's save as (with the addition of my own filter)? Is there anything special about Excel's save as dialog that is different than a regular save file dialog?
If so then I guess I must intercept any attempt to save with my own dialog. Right?

Funny you mention the release of COM objects (although this code was just a sample, not meant to be production quality), as this is your code... :)
Posted 26 Jul, 2012 11:57:20 Top
Eugene Astafiev


Add-in Express team


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

Of course, you can use the System.Windows.Forms.SaveFileDialog component for displaying your own dialog.

You are right: the sample code which I published on the forum doesn't include any ReleaseCOMObject statements due to the fact that it was pasted to illustrate how the task can be implemented. But in the production code you should always release underlying COM objects to avoid issues described in the When to release COM objects in Office add-ins developed in .NET article. That is why I always pay special attention to this.
.NET, Extended MAPI, Windows Phone & RT
Posted 27 Jul, 2012 05:06:57 Top