VSTO & .NET & Excel

November 25, 2007

Access running instances of Excel in VB

Filed under: .NET & Excel — Dennis M Wallentin @ 10:49 pm

When automating Excel from VB we can either create new instances or access already running instances. In different scenarios different methods may be needed in order to get workable solutions.

In the first scenario we may need to access a running instance and if not we need to create a new one as the following sample shows:


This is a rather simple method but it does the work and we can handle the situation if any error is raised.

In the second case we use a slightly different approach as the below code shows. The System.Diagnostic.Process class represents a Windows process. The sample uses the GetProcessesByName property to check if any Excel session is running or not. This approach is less ‘brutal’ then the above and offer also more options as it use the System.Diagnostic.Process (see also the other samples below):


In the third scenario another approach is used which is based on BindToMoniker. Unlike the above methods it works ‘botttom up’ (in the Excel’s object model) as it gets an interface pointer identified by the specified moniker, here the workbook file. The method provides the same functionality as the GetObject method in VB 6.    


In some scenarios we may want to start a new Excel session, allow the users to work with one or more workbooks and finally end the process. The following sample shows how we can achieve it:


I have not tested how it works when using the Dual Core Processor and instantiate new Windows processes.

The final sample shows how we can get information for all running Excel instances with the System.Diagnostic.Process class:


I have made the class module available as a textfile which can viewed or saved from here

Kind regards,



  1. Many thanks for this post I have been looking for this piece of information all across the net for ages !!!!!!!


    Comment by Olnac — May 21, 2008 @ 2:52 pm

  2. You’re welcome.

    Kind regards,

    Comment by Dennis Wallentin — May 21, 2008 @ 3:17 pm

  3. I needed to explain a developper how he could get an instance of an office application. I tried to explain but failed cause I lacked correct examples. Till I stumbled on this article. It helped me out greatly.

    From what I know – this article is 100% correct and extremely to the point.

    Fabulous work!

    Comment by Yllusyo — May 29, 2008 @ 10:51 am

  4. Hi,

    Many thanks for Your kind comment.

    Kind regards,

    Comment by Dennis Wallentin — May 29, 2008 @ 12:07 pm

  5. Hi Dennis,

    Thanks so much for this information. It really helps. However, is there anyway with the last method to get a reference to the excel object?? I am trying to get all the currently open excel workbooks so that the user can copy data into the main one from any of these (I am using a special copy function to convert the raw data into a different format, so I need the user to select it).

    Thanks for your help.

    Kind Regards,


    P.S. Sorry for dragging this article up again

    Comment by Ben — June 29, 2008 @ 11:43 am

  6. Hi Dennis,

    Your mechanism to get the process id of the running Excel instance by summing the process ids is clever, but it doesn’t handle the case where a new instance is launched between the initial check for running instances and the subsequent check after the “interesting” instance is launched. In that case, you will have multiple new instances to account for and the calculated process id will be wrong. The best way to get the process id of the “interesting” instance is to P/Invoke GetWindowThreadProcessId against the main window (obtained by calling Application.Hwnd) of the instance you care about.

    Comment by Geoff Darst — January 6, 2009 @ 6:41 pm

    • Geoff,

      Thanks for taking Your time to point it out. I will update the post accordingly when I’m done with the book project.

      Kind regards,

      Comment by Dennis Wallentin — January 6, 2009 @ 7:30 pm

  7. Thank you so much for the well thought out post. This is not easy information to find on the web.

    Comment by M. Bowman — August 12, 2009 @ 6:55 pm

  8. Dennis,

    Outstanding examples and explanations! I’ve been looking for a way to do several of these functions and you’ve laid out more than I needed, but not more than I could use. Thanks for sharing your wealth of “Net” knowledge.

    Have a great life!

    Comment by D. DeWitt — November 25, 2009 @ 8:50 pm

  9. Dennis,

    Awesome information that cannot be found on the Internet easily. Many thanks for sharing this for free. You are great!!!

    Comment by Yusuf S. — March 22, 2016 @ 1:29 am

    • Thanks for taking Your time to comment. I have received a lot of great feedback on this blog article and I’m happy that it serves its purpose so well.


      Comment by Dennis M Wallentin — March 22, 2016 @ 6:06 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: