none
Execute method of the Office SaveAs dialog raises an exception in Excel 2007 VSTO RRS feed

  • Question

  • Hi, I have a Visual Studio 2010 Excel 2007 Add-in project. I am using an English Excel version. My regional settings are Dutch.

    My code displays the Microsoft.Office.Core.MsoFileDialogType.msoFileDialogSaveAs dialog letting the user select the path and file format.

    private void Application_WorkbookBeforeSave(
      Microsoft.Office.Interop.Excel.Workbook workbook, 
      bool saveAsUI, ref bool cancel)
    {
      if (saveAsUI)
      {
        cancel = true;
    
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    
        var dialog = Globals.ThisAddIn.Application.get_FileDialog(
          Microsoft.Office.Core.MsoFileDialogType.msoFileDialogSaveAs);
    
        if (dialog.Show() != -1) return;
    
        dialog.Execute();
      }
    }
    

    But the Execute method of the dialog raises an COMException (80004005): "Error HRESULT E_FAIL has been returned from a call to a COM component."

    I am using the same method in my Word 2007 VSTO without problems and like to use this method inside of my Excel solution.

    I know that there are other methods of saving the workbook like other dialogs or the workbook's SaveAs method, but I want to save the file using the file format that the user selected in the 'Save as type' dropdown of the dialog. For example, if the user selected the CSV type I want to store the file as a CSV file.

    Thanks in advance!

    Michel Miranda

    Saturday, April 13, 2013 9:55 AM

Answers

  • yes, the build-in dialog doesn't have an execute method, but you can use show method to show the build-in dialog and save the file.

    And your code in my machine work fine and DVLJay also said your codes work fine in that thread, I suggest your office have some error, I suggest repail your office and try your codes again.




    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer.

    Sunday, April 14, 2013 5:37 AM
  • I think this problem is associate with save dialog, when developer opens-up another save dialog inside BeforeSave event, and it occurs only when word/excel has to stop opening its own dialog. I'm getting the similar COM Exception. but you can just ignore that exception by catching the exception with catch block.

    try
    {
    Dialog fileDialog = Application.Dialogs[WdWordDialog.wdDialogFileSaveAs];
                        int show = fileDialog.Show();
                        constint dialogResultOk = -1;
                        if (show == dialogResultOk) // means user pressed ok button 
                        {
    
    }
    
     
    
    }
    catch(Exception exception) 
    {
    // log exception
    }
    catch
    {
    
    }

    Here's the log of the exception that i get in Word. But I have just ignored that exception in my solution.

    2013-04-19 18:28:27,429: ERROR – Command failed
    System.Runtime.InteropServices.COMException (0x800A1066): Command failed
       at Microsoft.Office.Interop.Word.DocumentClass.Save()


    Faisal Ahmed Farooqui

    Monday, April 22, 2013 8:18 AM

All replies

  • I find  a thread realted to your problem, you can see the thread get solution

    Thanks


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer.

    Saturday, April 13, 2013 3:48 PM
  • Hi Learning hard,

    The thread you mentioned is indeed related to my problem, but it doesn't solve it.

    To my knowledge the built-in Excel dialog doesn't have an Execute method. I don't want the dialog to save the file.

    Thanks,

    Michel Miranda

    Sunday, April 14, 2013 5:09 AM
  • yes, the build-in dialog doesn't have an execute method, but you can use show method to show the build-in dialog and save the file.

    And your code in my machine work fine and DVLJay also said your codes work fine in that thread, I suggest your office have some error, I suggest repail your office and try your codes again.




    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer.

    Sunday, April 14, 2013 5:37 AM
  • I think this problem is associate with save dialog, when developer opens-up another save dialog inside BeforeSave event, and it occurs only when word/excel has to stop opening its own dialog. I'm getting the similar COM Exception. but you can just ignore that exception by catching the exception with catch block.

    try
    {
    Dialog fileDialog = Application.Dialogs[WdWordDialog.wdDialogFileSaveAs];
                        int show = fileDialog.Show();
                        constint dialogResultOk = -1;
                        if (show == dialogResultOk) // means user pressed ok button 
                        {
    
    }
    
     
    
    }
    catch(Exception exception) 
    {
    // log exception
    }
    catch
    {
    
    }

    Here's the log of the exception that i get in Word. But I have just ignored that exception in my solution.

    2013-04-19 18:28:27,429: ERROR – Command failed
    System.Runtime.InteropServices.COMException (0x800A1066): Command failed
       at Microsoft.Office.Interop.Word.DocumentClass.Save()


    Faisal Ahmed Farooqui

    Monday, April 22, 2013 8:18 AM