Search code examples
c#excelfilenamesexcel-interopsave-as

Showing xlworkbook saving dialog while closing suggesting name


I have a project where in my program opens a excel file called Template.xlsx and makes some updates in it and then when i run this line of code

xlWorkBook.Close(true, Type.Missing, Type.Missing);

I get the save as dialog box which is great but the problem is in the name field i get "Copy of Template.xlsx". I dont want that. I want to fill that field with for example "Order25.xlsx". is it possible?


Solution

  • Based on the suggestions in the comments, here is a possible solution using SaveFileDialog (you'll need using System.Windows.Forms):

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();  
    saveFileDialog1.Filter = "Excel|*.xls|Excel 2010|*.xlsx";;  
    saveFileDialog1.Title = "Save the Excel File";
    saveFileDialog1.FileName = "Order25.xlsx";
    
    if(saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        xlWorkBook.SaveAs(saveFileDialog1.FileName);
    } 
    

    I haven't tested it yet since I'm not near a computer, so let me know if it doesn't work.