Problem with EnumWindows function in Word 2007

V

viter.alex

I try to use following code but word crashes with error. In Word 2000
it works. Also it works in Vb6 Studio:
module 1:
Option Explicit

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As
Long, ByVal lParam As Long) As Long
Public Declare Function EnumDesktopWindows Lib "user32" (ByVal
hDesktop As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal
cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias
"GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias
"GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String,
ByVal nMaxCount As Long) As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As
Long) As Boolean
Dim nTextLen As Long
Dim sWinText As String
Dim sClassName As String
nTextLen = GetWindowTextLength(hwnd)
sWinText = Space(nTextLen)
sClassName = Space(254)
GetWindowText hwnd, sWinText, nTextLen + 1
GetClassName hwnd, sClassName, 255
Debug.Print "äÅÓËÒÉÐÔÏÒ: &H" & Hex(hwnd) & vbTab & _
"ôÅËÓÔ ÏËÎÁ: """ & sWinText & """" & vbTab & _
"éÍÑ ËÌÁÓÓÁ: """ & sClassName & """"
EnumWindowsProc = True
End Function


Call EnumWindows function:
Sub test()
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
 
T

Tony Jollans

Could you give a bit more detail - it works for me in Word 2007 running on
32-bit Windows Vista.

--
Enjoy,
Tony

www.WordArticles.com

I try to use following code but word crashes with error. In Word 2000
it works. Also it works in Vb6 Studio:
module 1:
Option Explicit

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As
Long, ByVal lParam As Long) As Long
Public Declare Function EnumDesktopWindows Lib "user32" (ByVal
hDesktop As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal
cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias
"GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias
"GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String,
ByVal nMaxCount As Long) As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As
Long) As Boolean
Dim nTextLen As Long
Dim sWinText As String
Dim sClassName As String
nTextLen = GetWindowTextLength(hwnd)
sWinText = Space(nTextLen)
sClassName = Space(254)
GetWindowText hwnd, sWinText, nTextLen + 1
GetClassName hwnd, sClassName, 255
Debug.Print "äÅÓËÒÉÐÔÏÒ: &H" & Hex(hwnd) & vbTab & _
"ôÅËÓÔ ÏËÎÁ: """ & sWinText & """" & vbTab & _
"éÍÑ ËÌÁÓÓÁ: """ & sClassName & """"
EnumWindowsProc = True
End Function


Call EnumWindows function:
Sub test()
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
 
V

viter.alex

Windows XP Professional 32-bit OS version 5.1.2600 Service Pack 2
build 2600
 
T

Tony Jollans

A bit more detail would still help - like what or where the error is.

It works for me on XP SP2 as well.
 
V

viter.alex

Strange thing happened. I tried many times. Error occurs after
GetWindowTextLength. GetLastError tells "Invalid Window Handle". I was
so disaappointed.
Then I paste this code to VB6, save it as module *.bas and import to
VBA. And it started to work. I don't know why, but it works now. I
hope in the future too

Dear Tony thanks for your attention
 
T

Tony Jollans

Glad you got it working. It sounds like it could have been some sort of
minor corruption that was cleared when you re-imported it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top