Denys Wessels
Denys Wessels

Reputation: 17019

Get a collection of all open Word applications

I would like to know whether there's a way to get a list of all Word instances open on the users machine?

I've tried this:

Process[]wordProcesses = Process.GetProcessesByName("WINWORD");

The line above always seems to be returning only one process, I suppose because only one MS Word process is running but many Word apps are open.

Upvotes: 0

Views: 2866

Answers (1)

Ian Gilroy
Ian Gilroy

Reputation: 2041

One approach here is to reach out and use AccessibleObjectFromWindow() to obtain a Microsoft.Office.Interop.Word.Application instance from each of the running instances of Word. This makes it easy to then query their Documents collections and do whatever else you need to do.

The answer on this SO question will get you really close: How to access Microsoft Word existing instance using late binding. That answer uses an IDispatch reference to manipulate the Application instance obtained from AccessibleObjectFromWindow() but you should be able to do something like this:

if (hr >= 0)
{
    var app = ptr.Application;
    foreach (var item in app.Documents)
        {
            var doc = (Document) item;
            Console.WriteLine(doc.FullName);
    }
}

Upvotes: 1

Related Questions