C# Saves two copies of a file!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

I've worked hard to get my data into Excel from a bunch of text files. I've
opened Excel and dumped the data in. I want to save the workbook/spreadsheet
but have not set a file name yet in the coding.

Running the below code I get a Dialog box that prompts for a file name and
path. You know the usual. I don't want this. I want to automate the entire
process. I want to create a filename that have MVC <Date>.xls.

If I cancel the dialog box I get an exception. If I put in a file name I
get my file (named as above) but also an additional file book1.xls for
example.

Is there a way to supress the dialog box and just save the file in the
naming convention that I want???

//Set default path and filename.
excel.DefaultFilePath = (@"C:\");
DateTime dtCurrTime = DateTime.Now;
string ExcelFile = "MVC " + dtCurrTime.ToShortDateString() + ".xls";
excel.Save(ExcelFile);
excel.Quit(); // Close Excel

Thanks JM
 
DisplayAlerts = false;

You should also use the SaveAs() method for files previously unsaved and set
Overwrite to true.

Dale Preston
 
Here is what I used. But the compiler complains

excel.ActiveWorkbook.SaveAs("c:\\MyWorkbook.xml",excel.XlFileFormat.xlXMLSpreadsheet,
Type.Missing, Type.Missing,Type.Missing, Type.Missing,
excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing);

excel.XlSaveAsAccessMode.xlNoChange & excel.XlFileFormat.xlXMLSpreadsheet

Any ideas as to what these constant values are? I actually want to save a
normal excel file too!
 
Never mind! I managed to figure out how to get the constants to work!

Here is an example!

string ExcelFile = "MVC " + dtCurrTime.ToShortDateString() + ".xls";

excel.ActiveWorkbook.SaveAs("c:\\" + ExcelFile,
Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing,
Type.Missing,Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 
I'm glad you got it. It saves me from digging deep into my archives for
when I last worked out saving Excel files from Visual Basic.

Dale
 
Back
Top