0

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;
    }
}
Salahudin Malik
  • 398
  • 4
  • 17
navi
  • 63
  • 2
  • 10
  • 2
    You have a single process with multiple windows. – SLaks Feb 05 '16 at 15:42
  • but in other files, like notepad, it gives multiple processes – navi Feb 05 '16 at 15:45
  • Yes, but Word is different. – SLaks Feb 05 '16 at 15:45
  • Why do you think the answer to the question you asked would help you solve your problem? Incidentally, what problem are you really trying to solve? – IInspectable Feb 05 '16 at 15:47
  • I want to get location(position) of each opened window. say for example i've 4 different files opened i can save their location and reopen them again. – navi Feb 05 '16 at 15:52
  • You can have multiple Word processes running, with each of them having multiple windows open. So, do you want to save the position of each of those windows? What can you do with that??? – Vlad Feinstein Feb 05 '16 at 16:25
  • right now i am unable to get each process separately. I want to save the position of file so i can reproduce the same file again on the same position. – navi Feb 05 '16 at 16:34
  • Your code above is getting position (coordinates) of the window; it has nothing about the files. Are you trying to reproduce the layout of the empty windows? – Vlad Feinstein Feb 05 '16 at 16:41

1 Answers1

1

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?

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490