Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel.Workbook.SaveAs(...) with a same name file

Tags:

c#

excel

com

I'm working with xls file. How can I save it (if exist) with the same file name + "(copy 1)" like Windows desktop.

method saveCommande(...)

        if(!Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "EE_Commande_Fournisseur"))
        {
            Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\EE_Commande_Fournisseur");
        }
        xlWorkBook.SaveAs("EE_Commande_Fournisseur\\" + path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, true, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\EE_Commande_Fournisseur\\" + path;

        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
        this.ReleaseComObject(xlApp,xlWorkBook);
        //sauvergarde dans la base de données
        _newAchatCommande.path = path;
        this._fileName = path;
        contexte.AddToAchatCommande(_newAchatCommande);
        contexte.SaveChanges();

thanks

like image 355
Yassine Avatar asked Aug 17 '13 13:08

Yassine


People also ask

What is the keyboard shortcut to re save a workbook with the same file name?

Ctrl + S will save the workbook using the previous name and location if your workbook has already been saved (i.e. it will overwrite the previous version).

Why does my Excel not save changes?

Possible reasons why documents don't save. Select the tab that applies to you, or go to the "Quick resolution" section. If you cannot save a workbook when you run Microsoft Excel in Windows Safe mode, the problem may be caused by a third-party add-in or by a file from one of the Excel startup locations.

How to save the current workbook with a different name in Excel?

Let’s see how we can save the current workbook with a different name. Follow the below steps to use Save As Function in Excel VBA: Step 1: Add a new module under Visual Basic Editor (VBE). Go to Insert and then select Module. Step 2: Define a new sub-procedure which can store a macro.

How to save a file with a new name in VBA?

In this VBA code, will use the GetSaveAsFilename function to save the file in a new location with a new name. Remember that, this code will allow you to specify the filename from Save As window of file explorer. And you have to write the file extension along with the filename in the corresponding box.

How to save a file with same name in activesheet?

In this code, ActiveSheet.SaveAs allows the file to be saved with the same name. As we have added the extension as.pdf at the end of the file, it gets exported into PDF file. You can see the image above for your reference.

How do I save a VBA file in Excel?

Excel VBA Save As If you are a frequent user of Microsoft Excel, you must have used Save As function under it, which allows you to save the currently opened workbook with a different name or different format (Excel Macro-enabled, CSV, PDF, etc.). You can also save the file in a different folder using this method.


1 Answers

try using File object's Exists method:

  if (!System.IO.File.Exists(@"C:\test2.xls"))
  {
   xlWorkBook.SaveAs(@"c:\test2.xls"); 
  }
  else
  {

   xlWorkBook.SaveAs(@"c:\test2(Copy).xls"); 

  }  

Or Alternatively you can overwrite your excel file by

   Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

  excel.DisplayAlerts = false;

  excelSheePrint.SaveAs(filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);

Note: It will overwrite the existing excel (if exist) without asking the user.

like image 189
Rohit Avatar answered Sep 19 '22 12:09

Rohit