私はGet specific window handle using Office interopで役立つものを見つけました。しかし、それらの答えは、あなたが新しく作成したウィンドウのハンドルを取得することに基づいています。私はそれを幾分変更して既存のウィンドウを取得し、それをユーティリティメソッドに埋め込んだ。
doc
は、現在のドキュメントです。
using System.Windows.Interop;
using System.Diagnostics;
public void SetOwner(System.Windows.Window pd)
{
var wordProcs = Process.GetProcessesByName("winword").ToList();
// in read-only mode, this would be e.g. "1.docx [Read-Only] - Microsoft Word"
var procs = wordProcs.Where(x =>
x.MainWindowTitle.StartsWith(Path.GetFileName(doc.FullName))
&&
x.MainWindowTitle.EndsWith("- Microsoft Word"));
if (procs.Count() >= 1)
{
// would prefer Word 2013's Window.HWND property for this
var handle = procs.First().MainWindowHandle;
WindowInteropHelper wih = new WindowInteropHelper(pd);
wih.Owner = handle;
}
}
残念ながら、プロセスの数が1より大きくなることはありませんので、(異なるフォルダに)同じ文書名で複数のウィンドウを説明するために可能ではないようです。しかし、私はそれが許容できる限界だと思います。