468,753 Members | 1,278 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,753 developers. It's quick & easy.

C# Saves two copies of a file!

JM
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
Nov 17 '05 #1
4 4855
DisplayAlerts = false;

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

Dale Preston

"JM" <JM@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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

Nov 17 '05 #2
JM
Here is what I used. But the compiler complains

excel.ActiveWorkbook.SaveAs("c:\\MyWorkbook.xml",e xcel.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!

"Dale Preston" wrote:
DisplayAlerts = false;

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

Dale Preston

"JM" <JM@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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


Nov 17 '05 #3
JM
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.xlWork bookNormal, Type.Missing,
Type.Missing,Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode. xlNoChange,Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

"Dale Preston" wrote:
DisplayAlerts = false;

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

Dale Preston

"JM" <JM@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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


Nov 17 '05 #4
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

"JM" <JM@discussions.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
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.xlWork bookNormal, Type.Missing, Type.Missing,Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode. xlNoChange,Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

"Dale Preston" wrote:
DisplayAlerts = false;

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

Dale Preston

"JM" <JM@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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


Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Henning Kage | last post: by
8 posts views Thread by qazmlp | last post: by
1 post views Thread by jsnX | last post: by
8 posts views Thread by Hank Reed | last post: by
1 post views Thread by brian.gabriel | last post: by
5 posts views Thread by Defected | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.