指定された文字列と一致するクラス名とウィンドウ名文字列を持つウィンドウのハンドルを取得(子ウィンドウの検索)を取得(FindWindowEx)
user32.dll の FindWindowEx を用いて、指定された文字列と一致するクラス名とウィンドウ名文字列を持つウィンドウのハンドルを取得(子ウィンドウの検索)します。
※サンプルを動作した環境の Microsoft Office のバージョンは 2003 です。
サンプルのダウンロード
■サンプルの説明

起動すると、こんな画面が表示されます。Button1 をクリックすると、Microsoft Access が起動していて、Access の Form が開かれている場合、Form のウィンドウテキストを表示します。
■コード
F0013_FindWindowEx.vb
Imports System.Runtime.InteropServices ''' <summary> ''' FindWindowEx(user32.dll) ''' </summary> Partial Public Class F0013_FindWindowEx ''' <summary> ''' 指定された文字列と一致するクラス名とウィンドウ名文字列を持つウィンドウのハンドルを取得(子ウィンドウの検索) ''' </summary> <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _ ByVal childAfter As IntPtr, _ ByVal lclassName As String, _ ByVal windowTitle As String) As IntPtr End Function <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function FindWindow(ByVal lpClassName As String, _ ByVal lpWindowName As String) _ As IntPtr End Function <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function GetWindowText(ByVal hwnd As IntPtr, _ ByVal lpString As System.Text.StringBuilder, _ ByVal cch As Integer) _ As Integer End Function <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) _ As Integer End Function ''' <summary> ''' Button1 ボタンクリック時のイベント ''' </summary> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' 親のウィンドウを検索 Dim parentHandle As IntPtr = FindWindow("OMAIN", "Microsoft Access") If parentHandle = IntPtr.Zero Then Return ' 子供のウィンドウを検索 Dim mdiClientHandle As IntPtr = FindWindowEx(parentHandle, IntPtr.Zero, "MDIClient", Nothing) If mdiClientHandle = IntPtr.Zero Then System.Diagnostics.Debug.WriteLine("Microsoft Access は起動していますが何も開いていません") Return End If ' 子供の子供ウィンドウを検索 Dim formHandle As IntPtr = FindWindowEx(mdiClientHandle, IntPtr.Zero, "OForm", Nothing) If formHandle = IntPtr.Zero Then System.Diagnostics.Debug.WriteLine("Microsoft Access は起動していますが Form は開いていません") Return End If ' Access の Form の文字数を取得→文字列を取得する Dim length As Integer = GetWindowTextLength(formHandle) If length > 0 Then Dim lpStringBuf As System.Text.StringBuilder lpStringBuf = New System.Text.StringBuilder("", length + 1) Dim ret As Integer = GetWindowText(formHandle, lpStringBuf, lpStringBuf.Capacity) If ret > 0 Then MessageBox.Show("Microsoft Access の " & lpStringBuf.ToString() & " が開いています") Return End If End If ' 名前取得失敗 System.Diagnostics.Debug.WriteLine("Microsoft Access の Form が開いています") End Sub End Class