I'm creating a new instance of Word using the Office interop by doing this:
var word = Microsoft.Office.Interop.Word.Application();
word.Visible = true;
word.Activate;
I can get a window handle like this:
var wordHandle = Process.GetProcessesByName("winword")[0].MainWindowHandle;
The problem is that code works on the assumption that there's no other instance of Word running. If there are multiple, it can't guarantee that the handle it returns is for the instance that I've launched. I've tried using GetForegroundWindow
after detecting a WindowActivate
event from my object but this is all running within a WPF application that's set to run as the topmost window, so I just get the handle to the WPF window. Are there any other ways to get the handle for my instance of word?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…