4

Background

I am automating some Office application (Word and PowerPoint) via command-line tool.

One thing my tool needs to do is locate all the running instances of Word.

I know how to get a reference to one of the instances...

Object running_obj = null;
{
    running_obj = System.Runtime.InteropServices.Marshal.GetActiveObject(progid);
}
catch (System.Exception)
{
    //failed to find the object;
}
if (running_obj!=null)
{
   var running_obj_type = System.Type.GetTypeFromProgID(progid);
   Microsoft.Office.Interop.Word.Application running_obj_wrapper;
   running_obj_wrapper = 
            (Microsoft.Office.Interop.Word.Application)
            System.Runtime.InteropServices.Marshal.CreateWrapperOfType(
                  running_obj, running_obj_type);
}

My question

How to find all the instances of the application I am looking for, not just one of them.

NOTE: Although my specifics question is about Office applications, am am also interested in answers that are more general.

namenlos
  • 5,111
  • 10
  • 38
  • 38

1 Answers1

2

Have not tried it. But it looks like the right solution. From Oliver Bock blog.

Igal Serban
  • 10,558
  • 3
  • 35
  • 40
  • This won't find Applications that don't have Documents open. I've tried the same thing for Word -- I have 5 Word apps open -- no documents in any of them (and 5 "WINWORD.EXE"s running under task manager) -- call this, and look for objects that cast to Application -- you find 5 of them -- but all 5 are the same object. -- If each has a document open, then you can enumerate those and find their .Applications; otherwise, you're SOL. – BrainSlugs83 Dec 02 '13 at 06:52