AW: Fensterhandle ermitteln
16.09.2015 19:48:53
Nepumuk
Hallo,
oder hast du Office2010 in der 64Bit-Version? Wenn ja, dann benutze folgenden Code:
Option Explicit
Private Declare PtrSafe Function EnumWindows Lib "user32.dll" ( _
ByVal lpEnumFunc As LongPtr, _
ByVal lParam As LongPtr) As Long
Private Declare PtrSafe Function GetWindowTextA Lib "user32.dll" ( _
ByVal hwnd As LongPtr, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare PtrSafe Function GetWindowTextLengthA Lib "user32.dll" ( _
ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function GetClassNameA Lib "user32.dll" ( _
ByVal hwnd As LongPtr, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
#If Win64 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long) As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long) As LongPtr
#End If
Private Const GWL_STYLE = (-16)
Private Const WS_VISIBLE = &H10000000
Private Const WS_BORDER = &H800000
Private llngRow As Long
Public Sub Start()
llngRow = 1
Columns("A:C").ClearContents
With Range("A1:C1")
.Value = Array("Hwnd", "Klasse", "Caption")
.Font.Bold = True
End With
Call EnumWindows(AddressOf WindowCallBack, ByVal 0&)
Columns("A:C").AutoFit
Tabelle1.Sort.SortFields.Clear
Tabelle1.Sort.SortFields.Add Key:=Columns(2)
With Tabelle1.Sort
.SetRange Columns("A:C")
.Header = xlYes
.MatchCase = False
.Apply
End With
End Sub
Private Function WindowCallBack(ByVal pvlngptrHwnd As LongPtr, ByVal lngptrParam As LongPtr) As Long
Dim strCaption As String, strClassName As String
Dim lngReturn As Long
Dim lngptrStyle As LongPtr
lngptrStyle = GetWindowLong(pvlngptrHwnd, GWL_STYLE)
If Cbool(lngptrStyle And (WS_VISIBLE Or WS_BORDER)) Then
strClassName = Space$(256)
lngReturn = GetClassNameA(pvlngptrHwnd, strClassName, 256)
strClassName = Left$(strClassName, lngReturn)
lngReturn = GetWindowTextLengthA(pvlngptrHwnd)
strCaption = Space$(lngReturn)
Call GetWindowTextA(pvlngptrHwnd, strCaption, lngReturn + 1)
llngRow = llngRow + 1
Cells(llngRow, 1).Resize(1, 3) = Array(pvlngptrHwnd, strClassName, strCaption)
End If
WindowCallBack = 1
End Function
Gruß
Nepumuk