1

I am developing a C# add in for Excel (using VSTO tools). I have an asynchronous process running, that pops up from time to time notification bubbles that are shown above all windows close to the taskbar.

I am able to bind an event to the click on these bubbles that pop up. If the Excel add in is hidden when the user clicks, I'd like to show the Excel window and navigate to a certain sheet.

Is this possible ?

BuZz
  • 16,318
  • 31
  • 86
  • 141

1 Answers1

6

You can find the Excel instance running thanks to this code:

oExcelApp =  (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

(more info on this thread: Get instance of Excel application with C# by Handle)

You may have to check if Excel is visible to the user:

oExcelApp .Visible = true;

And this snippet to activate the sheet you want:

oExcelApp.Worksheet sheet = (oExcelApp.Worksheet)this.Sheets["Sheet2"];
sheet.Select(Type.Missing);
Community
  • 1
  • 1
JMax
  • 26,109
  • 12
  • 69
  • 88
  • Thanks a lot for this. However, my users will have several excels open most likely. Is there a way to only show the concerned excel ? In my application, I have a ThisWorkBook.cs class under the .xls file. Can I somehow store a reference to this to show it later when needed ? – BuZz Nov 18 '11 at 09:01
  • 1
    @jerome G: define "concerned excel" - how will you decide which Excel process is the correct one? Especially when all of them have loaded your addin? – Doc Brown Nov 18 '11 at 09:07
  • see also this thread: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c-sharp/159419#159419 for good cleaning up your objects and some tips on how to select a Worksheet. Btw, are you sure you will open several **instances** of Excel? or several worksheets? – JMax Nov 18 '11 at 09:14
  • what I mean by concerned Excel instance is that the bubbles are thrown from the add in project. So I guess I want to select the Excel instance containing the instance of the add in throwing it, then get the worksheet, then get the sheet I want. – BuZz Nov 18 '11 at 09:39
  • and well, I have to assume users can have several instances of Excel. Of course all of them will have loaded the add in, but not all of them is throwing bubbles. The fact that I can bind a click event to call back withing the addin itself should help to achieve my goal right ? – BuZz Nov 18 '11 at 09:40
  • I don't know how you are throwing the Bubble but I think that's the best way to find the Excel instance through a call back on the click event. Thus, you will have the right Excel instance that has displayed the bubble. – JMax Nov 18 '11 at 10:56