请教各位老师我想枚举出类名为XLDESK的窗体内,类名为EXCEL7的工作簿标题名称,下面代码不通呢,谢谢        Dim hwndBook As Long, hwndEXCEL As Long, hwndXLDESK As Long, myMenu As Long
        hwndEXCEL = FindWindow("XLMAIN", xlapp.Caption)
        hwndXLDESK = FindWindowEx(hwndEXCEL, 0&, "XLDESK", vbNullString)
        hwndBook = FindWindowEx(hwndXLDESK, 0&, "EXCEL7", vbNullString) Private Sub Command1_Click()
    hwndXLDESK = FindWindowEx(hwndEXCEL, 0&, "XLDESK", vbNullString)
    hwndBook = GetWindow(hwndXLDESK, GW_CHILD)  '取得桌面窗口的第一个子窗口
   
    Do While hwndBook <> 0  '通过循环来枚举所有的窗口
        hwndBook = FindWindowEx(hwndXLDESK, 0&, "EXCEL7", vbNullString) '当前工作簿的句柄
              MyStr = String(100, Chr$(0))
              GetWindowText hwndBook, MyStr, 100
              wActivatew = Left$(MyStr, InStr(MyStr, Chr$(0)) - 1)
              
            MsgBox wActivatew '获取标题
        
        hwndBook = GetWindow(hwndXLDESK, GW_HWNDNEXT)  '取得下个窗口句柄
    Loop
End Sub

解决方案 »

  1.   

    标准模块:
    '标准模块
    Option ExplicitPrivate Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long'枚举顶级窗口
    Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
           Dim WindowClassName As String * 256
           Call GetClassName(hWnd, WindowClassName, 256)
           WindowClassName = Left(WindowClassName, InStr(WindowClassName, Chr(0)) - 1)
           If InStr(1, WindowClassName, "XLDESK") > 0 Then '查找类名为XLDESK的子窗口
              EnumChildWindows hWnd, AddressOf EnumChildWindowsProc, ByVal 0&
              EnumWindowsProc = False
           End If
           EnumWindowsProc = True
    End Function'枚举顶级窗口的子窗口
    Public Function EnumChildWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
           Dim WindowCaption As String, LengthCaption As Long, WindowClassName As String * 256
           Call GetClassName(hWnd, WindowClassName, 256)
           WindowClassName = Left(WindowClassName, InStr(WindowClassName, Chr(0)) - 1)
           If InStr(1, WindowClassName, "EXCEL7") > 0 Then '查找类名为EXCEL7的窗口
              LengthCaption = GetWindowTextLength(hWnd)
              WindowCaption = Space(LengthCaption)
              Call GetWindowText(hWnd, WindowCaption, LengthCaption + 1)
              MsgBox WindowCaption '取得类名为EXCEL7的窗口标题
              EnumChildWindowsProc = False
           End If
           EnumChildWindowsProc = True
    End Function窗体模块:
    Option ExplicitPrivate Sub Form_Load()
        EnumWindows AddressOf EnumWindowsProc, ByVal 0&
    End Sub