navi
navi

Reputation: 63

How to get All MS Word running process by name

I am trying to get all running MS Word processes, but it always returns 1. How I can get the exact number of processes? I do have more than one file open.

Process[] localByName = Process.GetProcessesByName("WINWORD");
foreach (Process p in localByName)
{ 
    if (!String.IsNullOrEmpty(p.MainWindowTitle))
    {
        Rect NotepadRect = new Rect();
        IntPtr ptr = p.MainWindowHandle;
        GetWindowRect(ptr, ref NotepadRect);
        objSchemeDetail.Top = NotepadRect.Top;
        objSchemeDetail.Bottom = NotepadRect.Bottom;
        objSchemeDetail.Left = NotepadRect.Left;
        objSchemeDetail.Right = NotepadRect.Right;
    }
}

Upvotes: 0

Views: 2934

Answers (1)

David Heffernan
David Heffernan

Reputation: 612844

The entire premise on which your question is based is wrong. You believe that each Word top level window is associated with a distinct process. That belief is incorrect. The architecture of Word has one process with multiple windows. This is simple enough to verify using a task manager program.

What you actually want to do is find all the top level windows associated with a specific process. That is a question that has been asked here many times before. For instance: How to enumerate all windows belonging to a particular process using .NET?

Upvotes: 1

Related Questions