Get Active Workbook

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
 
Check first that Excel is running with Process.GetProcessesByName("excel").
Get the Application object with Marshal.GetActiveObject("Excel.Application") and then get ActiveWorkbook.
 
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?
 
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.
 
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.
 
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?
 
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.
 
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;
    
}
 
@Frans : How is what you posted different from what has previously been described and shown on this thread?
 
needs to write data to an already open excel workbook.
You're going to struggle; excel locks its files upon opening to the extent that other apps can't even read them so you won't be able to do it by editing the file itself

Probably easiest to just start a timer and then tell the user then have 10 seconds to switch to make Excel the active app, then SendKeys the data you want. That can include keys like Ctrl+Home and Right Right Right to move to cell D1 etc
 
You're going to struggle; excel locks its files upon opening to the extent that other apps can't even read them so you won't be able to do it by editing the file itself

Probably easiest to just start a timer and then tell the user then have 10 seconds to switch to make Excel the active app, then SendKeys the data you want. That can include keys like Ctrl+Home and Right Right Right to move to cell D1 etc
Office automation is no problem, granted that it is ok with the interactive user. You can do whatever you want with the 'book' reference shown in post 8.

SendKeys is last resort for automation.
 
The code in post 8 works just fine. No need for dynamic late-binding.
 
Posts #2 says "and then get the ActiveWorkbook". It answers the question.

My response in post #4 asking about understanding vs just copying and pasting is relevant because this site meant to be a learning site -- not a question and answer site. And further in post #4, I redirected the user back to post #2 which is all the user actually needs to know if they are just seeking to understand (vs. just copy and paste code).

And then in post #6, I elaborated on what post #2 was describing in broad term. It also answers the question, since post #2 answered the question. Post #6 just goes into more details without giving the OP code that they can copy and paste. My second paragraph in post #6 was not about not using WinForms. It was about not putting the active workbook object directly into the WinForms combobox.

The code in post #8 works just fine without using dynamic
1669675201478.png


Your post #9 is still there. I don't know why you think it's been deleted.
 
Back
Top Bottom