5

In C# I am trying to get an instance of an Excel.Application object from a Process object. This seems like it should be really simple yet I cannot figure it out and cannot find an example. To repeat, I have a System.Diagnostics.Process object that I know refers to a running Excel instance. I now need to recover a Microsoft.Office.Interop.Excel.Application object that refers to the process so that I can go about manipulating the Excel application from C#.

In case it makes it any simpler, I also have the HWND id and window text associated with the active Excel window.

Thanks.

1

3 Answers 3

2

Answered on another SO post:

How to get Excel instance or Excel instance CLSID using the Process ID?

Sign up to request clarification or add additional context in comments.

2 Comments

I am not clear on how this is an answer, as the other thread gives you a IntPtr HWind of the Excel instance, but it still doesn't give you Microsoft.Office.Interop.Excel.Application object... Am I missing something?
Nevermind, the reason why I was confused was because the answer is actually related to an external link that does not work anymore. In fact, someone else posted the code @ pastebin.com/F7gkrAST
2

How to use Visual C# to automate a running instance of an Office program

using Excel = Microsoft.Office.Interop.Excel;
:    

private void button1_Click(object sender, System.EventArgs e)
{

    //Excel Application Object
    Excel.Application oExcelApp;

    this.Activate();

    //Get reference to Excel.Application from the ROT.
    oExcelApp =  (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

    //Display the name of the object.
    MessageBox.Show(oExcelApp.ActiveWorkbook.Name);

    //Release the reference.
    oExcelApp = null;
}

Not sure if you strictly need to retrieve application object from the Process class? Hope this helps.

1 Comment

Sorry, I should have mentioned that I have already considered this option. Unfortunately this common approach does not work for me because I need to be able to get the last active Excel window regardless of how many instances of Excel there are running. GetActiveObject will always deliver a reference to the same Excel instance even if there are multiple instances running.
1

I've created a class that can iterate through all running Excel instances, and also lookup Application instances by Hwnd, ProcessID, or a Process object.

http://www.codeproject.com/Tips/1080611/Get-a-Collection-of-All-Running-Excel-Instances

The answer is basically a lot of ugly extern calls to the Win32 API, which are best left hidden behind a clean public interface.

This has not been tested on all version of Excel or Windows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.