10

I could Open and Write to the excel file, but when I try to save the file by passing a path to it, the save operation prompts with the Save dialog. I was expecting it to quitely Save the file at the specified path

The code is as below:

excelApp.Save(exportToDirectory);
excelApp.Quit();

where, exportToDirectory is: "C:\files\strings.xlsx".

PS: I have already checked with the excel version and similar issue.

Thanks

7 Answers7

15

You need to use Workbook.SaveAs instead of Application.Save:

Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Add(missing);
...
wb.SaveAs(@"C:\temp\test.xlsx", missing, missing, missing, missing,
          missing, Excel.XlSaveAsAccessMode.xlExclusive,
          missing, missing, missing, missing, missing);
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • Any idea what the numerical value for Excel.XlSaveAsAccessMode.xlExclusive is? I need to pass this to this function in a windows script file. – MrVimes Jun 28 '13 at 13:33
  • 1
    @MrVimes Totally late to the party for you, however here they are for future reference: `public enum XlSaveAsAccessMode { xlNoChange = 1, xlShared = 2, xlExclusive = 3 }` – killercowuk Mar 09 '16 at 19:28
15

Setting the following properties might also help:

excelApp.DisplayAlerts = false;
excelApp.ScreenUpdating = false;
excelApp.Visible = false;
excelApp.UserControl = false;
excelApp.Interactive = false;
Bravax
  • 10,453
  • 7
  • 40
  • 68
3

just set the ActiveWorkbook.Saved property to true and you can Quit() without any Dialog box;

Gasy
  • 31
  • 1
2

Well, here's how Microsoft does it:

// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book1.xls", m_objOpt, m_objOpt, 
    m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, 
    m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

See one of their KB articles.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
1

myBook.Saved = true;
myBook.SaveCopyAs(xlsFileName);
myBook.Close(null, null, null);
myExcel.Workbooks.Close();
myExcel.Quit();

  • This worked best for me and not the highest rated answers. What I liked about this solution is that it didn't affect other Excel files opened by user. – ytoledano Nov 05 '16 at 20:30
1

I found excelApp.ActiveWorkbook.save() which can save the file, then I can quit without for asking.

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
Jay
  • 11
  • 1
1

ExcelApp.Interactive = false suppresses any dialog box.

excelApp.ActiveWorkbook.SaveAs(exportDirectory)

Vincent Van Den Berghe
  • 5,425
  • 2
  • 31
  • 40