Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
C#
private void button3_Click(object sender, EventArgs e)
        {
            string sFilename;
            //Excel Late Binding
            object objApp_Late;
            object objBook_Late;
            object objBooks_Late;
            object objSheets_Late;
            object objSheet_Late;
            object objRange_Late;
            object[] Parameters;
            try
            {
                // Get the class type and instantiate Excel.
                Type objClassType;
                objClassType = Type.GetTypeFromProgID("Excel.Application");
                objApp_Late = Activator.CreateInstance(objClassType);
                //Get the workbooks collection.
                objBooks_Late = objApp_Late.GetType().InvokeMember("Workbooks",
                BindingFlags.GetProperty, null, objApp_Late, null);
                //Add a new workbook.
                objBook_Late = objBooks_Late.GetType().InvokeMember("Add",
                    BindingFlags.InvokeMethod, null, objBooks_Late, null);
                //Get the worksheets collection.
                objSheets_Late = objBook_Late.GetType().InvokeMember("Worksheets",
                    BindingFlags.GetProperty, null, objBook_Late, null);
                //Get the first worksheet.
                Parameters = new Object[1];
                Parameters[0] = 1;
                objSheet_Late = objSheets_Late.GetType().InvokeMember("Item",
                    BindingFlags.GetProperty, null, objSheets_Late, Parameters);
                //*****SET Header******
                //**Get a range object that contains cell A1.
                Parameters = new Object[2];
                Parameters[0] = "A1";
                Parameters[1] = Missing.Value;
                objRange_Late = objSheet_Late.GetType().InvokeMember("Range",
                    BindingFlags.GetProperty, null, objSheet_Late, Parameters);
                //Case# cell A1.
                Parameters = new Object[1];
                Parameters[0] = "TESTA";
                objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty,
                    null, objRange_Late, Parameters);
                //ColumnWidth
                Parameters = new Object[1];
                Parameters[0] = 11;
                objRange_Late.GetType().InvokeMember("ColumnWidth", BindingFlags.SetProperty,
                    null, objRange_Late, Parameters);
                
                //End Header
                //START Set Data For each
                
                string aa;
                int ictr = 2;
                
                    //**Get a range object that contains cell A.
                    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);
                    //Value cell A
                    Parameters = new Object[1];
                    Parameters[0] = "TestAA";  //Case# (A)
                    objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty,
                        null, objRange_Late, Parameters);
                //END Set Data
                //Return control of Excel to the user.
                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);
                //Save
                sFilename = @"C:\TestA" + DateTime.Now.Date.ToString("ddMMyyyyHHNNSS") + ".xls";
                Parameters = new Object[12];
                Parameters[0] = sFilename;
                Parameters[1] = 56;//Excel.XlFileFormat.xlExcel8;
                Parameters[2] = Missing.Value;
                Parameters[3] = Missing.Value;
                Parameters[4] = false;
                Parameters[5] = false;
                //xlExclusive 	3
                //xlNoChange 	1
                //xlShared 	2
                Parameters[6] = 2;//Excel.XlSaveAsAccessMode.xlNoChange;
                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);
                //Quit - NO longer needed when Application(xmIF is Close Excel.EXE will be closed
                Parameters = new Object[1];
                Parameters[0] = true;
                objApp_Late.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, objApp_Late, null);
                objApp_Late = null;
                // ENSURE VARIABLES ARE DESTROYED & RELEASED AS IT GOES OUT OF SCOPE
                objRange_Late = null;
                objSheet_Late = null;
                objSheets_Late = null;
                objBook_Late = null;
                objBooks_Late = null;
                // CALL THE GARBAGE COLLECT METHOD
                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");
                
            }
        }
Posted
Updated 4-Aug-11 20:32pm
v2

1 solution

I manage to fix the problem by changing the

Parameters[1] = 39;//Excel.XlFileFormat.xlExcel8;

Parameters[6] = 1;//Excel.XlSaveAsAccessMode.xlNoChange;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900