Excel Automation

O

OutdoorGuy

Greetings,

I am attempting to use Excel automation to open a workbook, manipulate
some cells and then save the workbook. All is working well with the
exception of the "SaveAs" command. I am receiving several compile-time
errors using this code and can't figure out what is wrong. To verify
that the orignal syntax was correct, I recorded a macro (in Excel 2000)
and pasted the code in my routine. However, there is something
syntactically wrong that I can't figure out. Any ideas?

Thanks in advance!

********************************************************

private void btnAutomateExcel_Click(object sender, System.EventArgs e)
{
Excel.Application objExcel = new Excel.Application();
objExcel.Visible = true;
// Start a new workbook and a worksheet.
Excel.Workbook objBook = objExcel.Workbooks.Add
(System.Reflection.Missing.Value);
Excel.Worksheet objSheet = (Excel.Worksheet)
objBook.Worksheets.get_Item(1);

Excel.Range objRange;

objRange = objSheet.get_Range("A1",
System.Reflection.Missing.Value);
objRange.Value = "75";

objRange = objSheet.get_Range("B1",
System.Reflection.Missing.Value);
objRange.Value = "125";

objRange = objSheet.get_Range("C1",
System.Reflection.Missing.Value);
objRange.Value = "255";

objRange = objSheet.get_Range("D1",
System.Reflection.Missing.Value);
objRange.Value = "295";

objRange = objSheet.get_Range("E1",
System.Reflection.Missing.Value);

objRange.Value = "=SUM(RC[-4]:RC[-1])";

objRange = objSheet.get_Range("A1", "E1");
objRange.Font.Bold = true;

string strWorkbook = "C:\\Sample.xls";

// Code fails here.
objBook.SaveAs(Filename:=strWorkbook,
FileFormat:=xlNormal, Password:="",
WriteResPassword:="", ReadOnlyRecommended:=False,
CreateBackup:=False);

objExcel = null;
}
 
B

BIG_j

you have to specify ALL arguments in c#

object missing = System.Reflection.Missing.Value;

objBook.SaveAs(strWorkbook, missing (OR, for example,
Excel.XlFileFormat.xlXMLSpreadsheet), missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top