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
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).
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With