C# won't Late Bind

T

thejamie

I would like to add a late bound Excel version 5 object to a button on an asp
web page written in C#. It must be late bound because a late bound call to
excel 5 will correctly maintain compatibility with all versions of Microsoft
Word starting with version 5 going forward. C# apparently does not late
bind. I am able to get just so far with it then it refuses to recognize a
command:

objExcel.ActiveSheet.QueryTables.Add("OLEDB;" & strToExcel,
objExcel.Cells(1, 1), sql)
where strToExcel is the connection string and sql is the sql command string.

HOW IS THE LINE ABOVE CODED IN C#? If it cannot be properly coded then how
can I insert a vbscript into the web page and have the button_click event
access the vbscript in a C# application?

object objExcelApp;
object objExcelWKB;
object objExcelWKBs;
object objExcelSheets;
object objExcelSheet;
object objExcelRange;
object[] Parameters;

try
{
// Get the class type and instantiate Excel.
Type objClassType;
objClassType = Type.GetTypeFromProgID("Excel.Application");
objExcelApp = Activator.CreateInstance(objClassType);

//Get the workbooks collection.
objExcelWKBs = objExcelApp.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, objExcelApp, null);

//Add a new workbook.
objExcelWKB = objExcelWKBs.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod, null, objExcelWKBs, null);

//Get the worksheets collection.
objExcelSheets =
objExcelWKB.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, objBook_Late, null);

//Get the first worksheet.
Parameters = new Object[1];
Parameters[0] = 1;
objExcelSheet = objExcelSheets.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, objExcelSheets,
Parameters);

//Get a range object that contains cell A1.
Parameters = new Object[2];
Parameters[0] = "A1";
Parameters[1] = Missing.Value;
objExcelRange = objExcelSheet.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, objExcelSheet,
Parameters);

FROM HERE - Will not recognize:

objExcelRange.GetType().InvokeMember("ActiveSheet",
BindingFlags.SetProperty,
null, objExcelSheet, Parameters);
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top