Hello,
I created an application that uses the late binding in Excel. The application is working fine in Office 2010 and it save the file as Excel 2003 version but when i install it in Office 2003 it causes an error "Exception has been thrown by the target of an invocation. Line: mscorlib"
when i tried to debug the application in Office 2003 the error is encountered in this line :
objBook_Late = objBooks_Late.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, objBook_Late, Parameters);
Hope someone can help me on this, Many thanks in Advance
below is my sample code
private void button3_Click(object sender, EventArgs e)
{
string sFilename;
object objApp_Late;
object objBook_Late;
object objBooks_Late;
object objSheets_Late;
object objSheet_Late;
object objRange_Late;
object[] Parameters;
try
{
Type objClassType;
objClassType = Type.GetTypeFromProgID("Excel.Application");
objApp_Late = Activator.CreateInstance(objClassType);
objBooks_Late = objApp_Late.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, objApp_Late, null);
objBook_Late = objBooks_Late.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod, null, objBooks_Late, null);
objSheets_Late = objBook_Late.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, objBook_Late, null);
Parameters = new Object[1];
Parameters[0] = 1;
objSheet_Late = objSheets_Late.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, objSheets_Late, Parameters);
Parameters = new Object[2];
Parameters[0] = "A1";
Parameters[1] = Missing.Value;
objRange_Late = objSheet_Late.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, objSheet_Late, Parameters);
Parameters = new Object[1];
Parameters[0] = "TESTA";
objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty,
null, objRange_Late, Parameters);
Parameters = new Object[1];
Parameters[0] = 11;
objRange_Late.GetType().InvokeMember("ColumnWidth", BindingFlags.SetProperty,
null, objRange_Late, Parameters);
string aa;
int ictr = 2;
Parameters = new Object[2];
Parameters[0] = "A" + ictr.ToString();
Parameters[1] = Missing.Value;
objRange_Late = objSheet_Late.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, objSheet_Late, Parameters);
Parameters = new Object[1];
Parameters[0] = "TestAA";
objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty,
null, objRange_Late, Parameters);
Parameters = new Object[1];
Parameters[0] = false;
objApp_Late.GetType().InvokeMember("Visible", BindingFlags.SetProperty,
null, objApp_Late, Parameters);
objApp_Late.GetType().InvokeMember("UserControl", BindingFlags.SetProperty,
null, objApp_Late, Parameters);
sFilename = @"C:\TestA" + DateTime.Now.Date.ToString("ddMMyyyyHHNNSS") + ".xls";
Parameters = new Object[12];
Parameters[0] = sFilename;
Parameters[1] = 56;
Parameters[2] = Missing.Value;
Parameters[3] = Missing.Value;
Parameters[4] = false;
Parameters[5] = false;
Parameters[6] = 2;
Parameters[7] = Missing.Value;
Parameters[8] = Missing.Value;
Parameters[9] = Missing.Value;
Parameters[10] = Missing.Value;
Parameters[11] = Missing.Value;
objBook_Late = objBooks_Late.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, objBook_Late, Parameters);
Parameters = new Object[1];
Parameters[0] = true;
objApp_Late.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, objApp_Late, null);
objApp_Late = null;
objRange_Late = null;
objSheet_Late = null;
objSheets_Late = null;
objBook_Late = null;
objBooks_Late = null;
GC.Collect();
GC.WaitForPendingFinalizers();
objApp_Late = null;
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}