2
votes

I am trying to write a program that will open multiple documents with a single click and specify size and location for each individual document window. I was having decent success with a rudimentary program to test the opening and positioning operations until I tried to open a second Word or Excel document.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

        static void Main(string[] args)
        {
            Process resize = new Process();

            resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\TEST1.txt";
            resize.Start();
            resize.WaitForInputIdle();
            MoveWindow(resize.MainWindowHandle, 10, 10, 500, 500, true);

            resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSWTEST1.docx";
            resize.Start();
            resize.WaitForInputIdle();
            MoveWindow(resize.MainWindowHandle, 20, 20, 500, 500, true);

            resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSXTEST1.xlsx";
            resize.Start();
            resize.WaitForInputIdle();
            MoveWindow(resize.MainWindowHandle, 30, 30, 500, 500, true);

            resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\TEST2.txt";
            resize.Start();
            resize.WaitForInputIdle();
            MoveWindow(resize.MainWindowHandle, 40, 40, 500, 500, true);

            resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSWTEST2.docx";
            resize.Start();
            resize.WaitForInputIdle();
            MoveWindow(resize.MainWindowHandle, 50, 50, 500, 500, true);

            resize.StartInfo.FileName = "C:\\Users\\Pete\\Desktop\\MSXTEST2.xlsx";
            resize.Start();
            resize.WaitForInputIdle();
            MoveWindow(resize.MainWindowHandle, 60, 60, 500, 500, true);
         }
    }
}

The program tries to open two .txt files using Notepad, two .docx files using MSWord, and two .xlsx files using MSExcel. No matter what order I open the documents in the program throws an InvalidOperationException on the WaitForInputIdle line immediately after opening a second Word or Excel file. Any help fixing this error would be greatly appreciated.

3
Does the same issue arise if you don't reuse resize, i.e., have a different Process for each?Austin Salonen
Why would you Start() multiple documents in the SAME process (resize)?paparazzo
@Austin - Yes the same issue arises whether I use 1 process or 6 processes.Pete Hogrefe
OR you could do it via static: Process proc = Process.Start(Execut, args);. This way you know you will always have a new window. Edit: Didn't test it with office files, only chrome.StinkyCat

3 Answers

5
votes

When you try to open a Word or Excel document the application (depending on version) being executed simply looks for the same application already running, asks it to open a new "window" and shuts down. This means the application you're actually running never really gets a message pump--which leads to WaitForInputIdle thowing an InvalidOperationException (as documented)

I suggest you simply try to catch and ignore the exception--I'm not sure of any way to tell if Word/Excel successfully opened the document via Process.Start Update: conceptually, If you do get the exception that means Word/Excel found another running instance and switched to it--so, presumably that's some degree of "success".

3
votes

When you open the second Word or Excel document, the launching process detects that Word/Excel is already started and simply sends the other process the document information and shuts down.

A simpler application, such as notepad, does not have that behavior.

That means that, in that case, the Process instance that you have the second time you launch is worth precisely zilch in order to control the document which is being opened.