none
XLL creating SaveAsDialog (like Application.GetSaveAsFileName) RRS feed

  • Question

  • Hi.

    I have an XLL I've created.  One of the functions save some intermediate data in a text file (sort of a log of the results of certain steps in the calculation).  I want to open a "Save As" dialog box to allow the name of the text file to be supplied.  I've managed to accomplish this using two different methods but each method has some minor quirks and I am wondering if there is any way around these issues.

    Ideally I would like the dialog box to appear exactly as the one used by Application.GetSaveAsFileName.  But, if the file selected already exists, I'd like a prompt to appear making the user confirm overwriting the file.

    (#1) I can accomplish this using by calling xlfSaveDialog.  The first issue with this method is that if a file name is entered that doesn't exist, it is enclosed in quotes.  (If the file already exists this doesn't seem to happen.)  Not a big issue but not how the GetSaveAsFileName dialog behaves and makes the file name look a little strange.  The second issue is that I can only make a prompt appear after the dialog is already closed.  (This issue is really minor and I can easily loop back to the xlfSaveDialog call if the user chooses not to overwrite the file.)  So does anyone know a method for calling GetSaveAsFileName from an XLL?  Or how I can get rid of the quotes around the file name when I use xlfSaveDialog?

    (#2) I can also accomplish this using the C++ Windows API function GetSaveFileName.  By setting one of the flags in the OPENFILENAME structure which that function uses, I can make it automatically prompt if the file already exists which is nice.  But it doesn't have the same "look and feel" of the Excel save dialogs.  And it only will add a default 3 character extension, like ".txt" but not ".text".  The OPENFILENAME structure does have fields for specifying a dialog template.  Is there some way I can use a dialog template stored within Excel to make the dialog appear more like Excel's own Save As dialog?

    I realize both of the questions are sort of nitpicking about minor issues but since I wasn't able to find a whole lot of documentation about any of this I was hoping some of the experts here might have an idea or two.

    Thanks.

    Monday, August 25, 2014 3:00 AM

All replies

  • Hi Kmart,

    >> The first issue with this method is that if a file name is entered that doesn't exist, it is enclosed in quotes

    I’m not able to reproduce this issue in Excel 2013 with the code below. Would you mind sharing you sample code and Excel version to help us reproduce this issue?

    >> The second issue is that I can only make a prompt appear after the dialog is already closed.

    I think it is because the “Save As” dialog is a modal dialog and I'm afraid it is by-design.

    >> Is there some way I can use a dialog template stored within Excel to make the dialog appear more like Excel's own Save As dialog

    As far as I know, there are no such kind of Official dialog templates in Visual Studio.

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 26, 2014 9:27 AM
    Moderator
  • Thanks for replying.  I am using Excel 2007 (along with the Excel 2007 SDK in case that matters).

    I did some further testing and it seems that whatever is causing the quotes around the filename is more complicated than the file not existing.  Try using your sample code except provide an initial file name, like so:

    XLOPER12 xRes, xMissing, xName, xFilter;
    
    xMissing.xltype = xltypeMissing;
    xName.xltype = xltypeStr;
    xName.val.str = L"\011test.html";
    xFilter.xltype = xltypeMissing; // you'll see why I include this later
    
    // I use 4 arguments instead of 5 because I omit the filter index
    Excel12f(xlfSaveDialog, &xRes, 4, &xName, &xMissing, &xMissing, &xFilter); 
    
    return 0;
    

    When I execute that I get "test.html" as the initial file in the dialog box (including the quotes).

    If I change the definition of the filter, instead of using the xltypeMissing, to something like:

    // This one doesn't cause quotes
    xFilter.xltype = xltypeStr;
    xFilter.val.str = L"\033Html files (*.html), *.html";
    
    // This one, explicitly settings All files, causes quotes again.
    xFilter.xltype = xltypeStr;
    xFilter.val.str = L"\016All files, *.*"
    
    
    

    As far as the dialog template, if you look at the definition of the OPENFILENAME structure used by the Windows API function GetSaveFileName, it allows me to use the hInstance of a module along with a string name of a dialog box template.  So I wondered if it would be possible to use the hInstance of the Excel window (xlGetInst) and some string name to get the template for the dialog box used by Application.GetSaveAsFileName?

    Tuesday, August 26, 2014 12:05 PM