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: 577
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


Guest


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 http://www.add-in-express.com/creating-addins-blog/2012/02/16/word-dialogues-programmatically/ article on our technical blog.
Posted 25 Jul, 2012 15:02:55 Top
nwein




Posts: 577
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


Guest


Hi Nir,

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

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


The http://msdn.microsoft.com/en-us/library/microsoft.office.core.filedialogfilters.add.aspx method returns an instance of the FileDialogFilter class which should be released after.

http://msdn.microsoft.com/en-us/library/office/aa219834%28v=office.11%29.aspx 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.
Posted 26 Jul, 2012 11:37:21 Top
nwein




Posts: 577
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 http://www.add-in-express.com/forum/read.php?FID=1&TID=6370... :)
Posted 26 Jul, 2012 11:57:20 Top
Eugene Astafiev


Guest


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 http://www.add-in-express.com/creating-addins-blog/2008/10/30/releasing-office-objects-net/ article. That is why I always pay special attention to this.
Posted 27 Jul, 2012 05:06:57 Top