Hi Cnathaide,
Thanks for your reply.
In your follow-up question, you now have different versions of Excel from
Excel 97 to Excel XP, and you want to your app work properly on all
versions.
Basically, using an assembly generated from excel 97 type library should
work on the later version.
If you will be doing a lot of calls to Excel, I recommend you use a
language such
as VB.NET or JScript that is designed for this. Although it is possible in
C#,
it's not as easy, you take care and set Type.Missing for every optional
parameters.
If you want to write in C# using reflection, you may use the static method
Type.InvokeMember to use this com object using reflection.
<code>
object comObj = Marshal.GetActiveObject("ProgID");
Type t = Type.GetTypeFromProgID("ProgID");
object [] = new object []{arg1,arg2,...};
t.InvokeMember("someMethod",BindingFlags,InvokeMethod,null, comObj, args);
</code>
Here is another code snippet which shows you how to use excel using
late-binding in VB.NET.
<code>
Option Strict Off
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
// auto generated code omitted
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim XlApp As Object
Dim XlBook As Object
Dim XlWorkBooks As Object
Dim XlSheet As Object
XlApp = CreateObject("Excel.Application")
XlApp.ScreenUpdating = False
'Late bind an instance of an Excel workbook.
XlWorkBooks = XlApp.Workbooks
XlBook = XlWorkBooks.Add
'late bind an instance of an excel worksheet.
XlSheet = XlBook.worksheets(1)
XlSheet.activate()
XlApp.screenupdating = True
XlApp.usercontrol = True
Dim ApplicationObject As Object
ApplicationObject = XlSheet.application ' show the application.
ApplicationObject.visible = True
'XlApp.Quit()
Marshal.ReleaseComObject(ApplicationObject)
Marshal.ReleaseComObject(XlSheet)
Marshal.ReleaseComObject(XlBook)
Marshal.ReleaseComObject(XlWorkBooks)
Marshal.ReleaseComObject(XlApp)
End Sub
End Class
</code>
If you still have questions on this issue, please be free to reply this
thread.
Thanks!
Best regards,
Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.