J
jhh
I'm trying to delete a worksheet from a workbook using C# and late
binding. What ever I try, I don't seem to get rid of the sheet. I get
no errors or no messages.
Below is the basic of the code i try to run. Can anybody please
enlighten me?
object oExcelApp = null;
object oExcelWorkbooks;
object oExcelWorkbook = null;
object oExcelSheets;
object oExcelSheet;
object[] Parameters;
int numberOfSheets = 0;
Type objClassType;
// Get the class type and instantiate Excel.
objClassType = Type.GetTypeFromProgID("Excel.Application");
oExcelApp = Activator.CreateInstance(objClassType);
//Get the workbooks collection.
oExcelWorkbooks = oExcelApp.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, oExcelApp, null);
// Open a workbook
Parameters = new object[15];
Parameters[0] = @"C:\TestSet.xls"; ;
Parameters[3] = 5; // Format = Nothing.
Parameters[1] = Parameters[2] = Parameters[4] = Parameters[5] =
Parameters[6] = Parameters[7] = Parameters[8] = Parameters[9] =
Parameters[10] =
Parameters[11] = Parameters[12] = Parameters[13] = Parameters[14] =
Type.Missing;
oExcelWorkbook = oExcelWorkbooks.GetType().InvokeMember("Open",
BindingFlags.InvokeMethod, null, oExcelWorkbooks, Parameters);
oExcelSheets = oExcelWorkbook.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, oExcelWorkbook, null);
numberOfSheets = (int)oExcelSheets.GetType().InvokeMember("Count",
BindingFlags.GetProperty, null, oExcelSheets, null);
int n;
String sheet;
Parameters = new object[1];
for (n = 1; n <= numberOfSheets; n++)
{
Parameters[0] = n;
//Get the first worksheet.
oExcelSheet = oExcelSheets.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, oExcelSheets, Parameters);
sheet = (String)oExcelSheet.GetType().InvokeMember("Name",
BindingFlags.GetProperty, null, oExcelSheet, null);
if ( String.Compare( sheet, "noleap", true) == 0)
{
// Delete the sheet!
oExcelSheet.GetType().InvokeMember("Delete",
BindingFlags.InvokeMethod, null, oExcelSheet, null);
}
}
Parameters = new object[3];
Parameters[0] = Parameters[1] = Parameters[2] = Type.Missing;
if (oExcelWorkbook != null)
{
oExcelWorkbook.GetType().InvokeMember("Save",
BindingFlags.InvokeMethod, null, oExcelWorkbook, null);
oExcelWorkbook.GetType().InvokeMember("Close",
BindingFlags.InvokeMethod, null, oExcelWorkbook, Parameters);
}
if (oExcelApp != null)
{
oExcelApp.GetType().InvokeMember("Quit",
BindingFlags.InvokeMethod, null, oExcelApp, null);
}
binding. What ever I try, I don't seem to get rid of the sheet. I get
no errors or no messages.
Below is the basic of the code i try to run. Can anybody please
enlighten me?
object oExcelApp = null;
object oExcelWorkbooks;
object oExcelWorkbook = null;
object oExcelSheets;
object oExcelSheet;
object[] Parameters;
int numberOfSheets = 0;
Type objClassType;
// Get the class type and instantiate Excel.
objClassType = Type.GetTypeFromProgID("Excel.Application");
oExcelApp = Activator.CreateInstance(objClassType);
//Get the workbooks collection.
oExcelWorkbooks = oExcelApp.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, oExcelApp, null);
// Open a workbook
Parameters = new object[15];
Parameters[0] = @"C:\TestSet.xls"; ;
Parameters[3] = 5; // Format = Nothing.
Parameters[1] = Parameters[2] = Parameters[4] = Parameters[5] =
Parameters[6] = Parameters[7] = Parameters[8] = Parameters[9] =
Parameters[10] =
Parameters[11] = Parameters[12] = Parameters[13] = Parameters[14] =
Type.Missing;
oExcelWorkbook = oExcelWorkbooks.GetType().InvokeMember("Open",
BindingFlags.InvokeMethod, null, oExcelWorkbooks, Parameters);
oExcelSheets = oExcelWorkbook.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, oExcelWorkbook, null);
numberOfSheets = (int)oExcelSheets.GetType().InvokeMember("Count",
BindingFlags.GetProperty, null, oExcelSheets, null);
int n;
String sheet;
Parameters = new object[1];
for (n = 1; n <= numberOfSheets; n++)
{
Parameters[0] = n;
//Get the first worksheet.
oExcelSheet = oExcelSheets.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, oExcelSheets, Parameters);
sheet = (String)oExcelSheet.GetType().InvokeMember("Name",
BindingFlags.GetProperty, null, oExcelSheet, null);
if ( String.Compare( sheet, "noleap", true) == 0)
{
// Delete the sheet!
oExcelSheet.GetType().InvokeMember("Delete",
BindingFlags.InvokeMethod, null, oExcelSheet, null);
}
}
Parameters = new object[3];
Parameters[0] = Parameters[1] = Parameters[2] = Type.Missing;
if (oExcelWorkbook != null)
{
oExcelWorkbook.GetType().InvokeMember("Save",
BindingFlags.InvokeMethod, null, oExcelWorkbook, null);
oExcelWorkbook.GetType().InvokeMember("Close",
BindingFlags.InvokeMethod, null, oExcelWorkbook, Parameters);
}
if (oExcelApp != null)
{
oExcelApp.GetType().InvokeMember("Quit",
BindingFlags.InvokeMethod, null, oExcelApp, null);
}