How to locate a window by title
-
Bit of a long winded question I'm afraid. I have an app which sends data to an open document displayed by an app such as Word, using a simple paste operation. This requires that I locate the handle of the window associated with the document. The user configures the file location of the application that displays the document, and the file location of the document. Obtaining the window handle is usually quite easy using something like this:
using (var searcher = new System.Management.ManagementObjectSearcher("SELECT ProcessId, ExecutablePath FROM Win32_Process"))
{
using (var results = searcher.Get())
{
foreach (System.Management.ManagementObject item in results)
{
string ExecutablePath = (string)item["ExecutablePath"];
if (!string.IsNullOrEmpty(ExecutablePath) && (ExecutablePath == _destinationApplication.DestinationApplicationPath))
{
int processID = (int)(uint)item["ProcessId"];
System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID);if ((process != null) && process.MainWindowTitle.Contains(documentTitle)) { return process.MainWindowHandle; } } }
}
}Unfortunately Microsoft Word is a pain as one instance manages multiple open documents. Calling GetWindowText() on the main window handle gets the name of one document. Using Spy to look at the window tree for Word shows that none of the child windows store the name of the other document as the window text, even though I can see the name of the document displayed by a window. So, how do I locate the handle of the window in which the second document is displayed?
-
Bit of a long winded question I'm afraid. I have an app which sends data to an open document displayed by an app such as Word, using a simple paste operation. This requires that I locate the handle of the window associated with the document. The user configures the file location of the application that displays the document, and the file location of the document. Obtaining the window handle is usually quite easy using something like this:
using (var searcher = new System.Management.ManagementObjectSearcher("SELECT ProcessId, ExecutablePath FROM Win32_Process"))
{
using (var results = searcher.Get())
{
foreach (System.Management.ManagementObject item in results)
{
string ExecutablePath = (string)item["ExecutablePath"];
if (!string.IsNullOrEmpty(ExecutablePath) && (ExecutablePath == _destinationApplication.DestinationApplicationPath))
{
int processID = (int)(uint)item["ProcessId"];
System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID);if ((process != null) && process.MainWindowTitle.Contains(documentTitle)) { return process.MainWindowHandle; } } }
}
}Unfortunately Microsoft Word is a pain as one instance manages multiple open documents. Calling GetWindowText() on the main window handle gets the name of one document. Using Spy to look at the window tree for Word shows that none of the child windows store the name of the other document as the window text, even though I can see the name of the document displayed by a window. So, how do I locate the handle of the window in which the second document is displayed?
-
Enumerate all processes, and then each window from each proces. c# - How to enumerate all windows belonging to a particular process using .NET? - Stack Overflow[^]
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
Unfortunately that does not work as I cannot reliably identify which window belongs to the target document. And yes I do already use that method to enumerate through the child windows.