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.
resize
, i.e., have a differentProcess
for each? – Austin SalonenProcess 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