Get Active Workbook with c#

jrdnoland

Member
Joined
May 15, 2016
Messages
9
Programming Experience
10+
I'm looking for the easiest way to get the active workbook of Excel with c#.
I have a small c# app that needs to write data to an already open excel workbook.
The program will then copy data from c# Comboxes into certain rows in a certain column of the active sheet of the active workbook.
Most of the documentation and videos I've seen are loading an excel file and then doing something with it. I need to work with an already open excel workbook, it is possible that the user may have multiple excel workbooks open at the time.
I'm working with Excel 2016 (32 and 64 bit) and visual studio 2017.
Thanks, Jeff
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,265
Location
Norway
Programming Experience
10+
Check first that Excel is running with Process.GetProcessesByName("excel").
Get the Application object with Marshal.GetActiveObject("Excel.Application") and then get ActiveWorkbook.
 

jrdnoland

Member
Joined
May 15, 2016
Messages
9
Programming Experience
10+
I can't seem to get the activeworkbook or the sheet or much of anything else.
Can someone please point me to some books or tutorials that will help me understand this better?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
Are you looking to understand? Or looking to copy-and-paste some code? Post #2 explains the steps needed to accomplish the task that you asked about, assuming that you have the rudimentary understanding of C# and Office InterOp.
 

jrdnoland

Member
Joined
May 15, 2016
Messages
9
Programming Experience
10+
Both actually, I'd like to understand how to use the object browser to find the objects that I need. I would also like some form of documentation (Ideally with examples) or using the excel interop. I understand the basics of c#.

I need to get whatever workbooks happen to be open into a combobox and then each workbooks worksheets into another combobox. Finally I want the program to detect which column of which worsheet the user is in and to transfer information from my c# portion into that active column at specific rows.

This is all I have so far:

C#:
public Microsoft.Office.Interop.Excel.Workbook ActiveWorkbook { get; }
        private void button1_Click(object sender, EventArgs e)
        {
            Process.GetProcessesByName("excel");

            Marshal.GetActiveObject("Excel.Application");
            comboBox1.Items.Add(ActiveWorkbook);
        }

The code gets to the last line then throws an exception.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
The Process.GetProcessesByName() lets you check to see if Excel is already running. You need to check the returned value to see if Excel is found. If it is running, then you need to call Marshal.GetActiveObject() to get the main Excel COM server instance's interface as an Application. From that Application object, you can get the ActiveWorkbook object/interface.

As a quick aside, it's not really a good idea to add an object directly into a WinForms UI element. You'll likely want to get the string representation of the active workbook. Perhaps it's name?
 

jrdnoland

Member
Joined
May 15, 2016
Messages
9
Programming Experience
10+
Thank you for your input. I've been on another project for a bit and am now back to this issue. I will try to implement what you have said. I realize my wording must have been misleading, but yes, I realize that I'm trying to add a string expression of the object.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,265
Location
Norway
Programming Experience
10+
Process.GetProcessesByName("excel"); Marshal.GetActiveObject("Excel.Application");
These are methods that return a value, and it's that value you have to work with.
usings:
using System.Diagnostics;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
C#:
if (Process.GetProcessesByName("excel").Length > 0)
{
    var app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
    var book = app.ActiveWorkbook;
    
}
 
Top Bottom