Reference to existing Excel object

C

CA

Hi,

I am trying to get a reference to an existing Excel application
object. In other words, instead of

object oExcel = new Excel.Application();

I would like to write something like

Process[] processes = Process.GetProcessesByName("EXCEL");
Process proc = proc[0];
// Here is where I would like to retrieve the object of
// type "Excel.Application". But it is not clear how I retrieve this
// from the Process object.


Any help is greatly appreciated. Thanks in advance.

Regards

Chris
 
Y

Ying-Shen Yu[MSFT]

Hi cnathaide,

You may try using the Marshal.GetActiveObject method like the follow
snippet:
<code>
//Get a running instance of Excel (throws COMExeption if not running
already)
Excel.Application app =
(Excel.Application)Marshal.GetActiveObject("Excel.Application");
</code>

Note you need import the type library of Excel, for Office XP and above ,
you may use the Office XP PIA as it provides the official description of
the most commonly used Microsoft Office XP type libraries. These Microsoft
Office XP PIAs make interoperability easier between managed code and Office
XP COM type libraries. You may refer to the follow link to get the PIA:
http://support.microsoft.com/default.aspx?kbid=328912

Does my reply solve your problem?
If you still have problem on it please reply to group, I'll follow up with
you.
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.
 
C

CA

Ying-Shen,

Thanks for the reply. I tried it and it worked.

I have a follow-up question: In determining which Excel library to
include in my application, I have come up with a problem. Different
users have different versions of Excel. Some of them are still using
Excel 97. Is there any way I can guarantee that a COM library would
work for all Excel applications from Excel 97 to Excel XP.

If not, I was thinking of using Reflection to query the object and
invoke the methods that I need. (I only need a few basic methods.) Can
this be done after I have a reference to the COM object. I went and
look at the object in the debugger, but it was not clear how I proceed
in this manner.

Thanks once again for your help.

Regards

Chris
 
Y

Ying-Shen Yu[MSFT]

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.
 

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

Top