Gasman
Enthusiastic Amateur
- Local time
- Today, 09:28
- Joined
- Sep 21, 2011
- Messages
- 16,450
Hi all,
In attempting to help another member on another site, ChatGPT gave me code but for 64bit, and I am on 32bit Office.
I amended that code to have the conditional compile directive VBA7, and that appeared to work.
The code was to close a particular window with a known title.
I then thought about finding out the actual name of the title of the window we are loooking for, which generally is in the form Filename - Application Name.
However despite using VBA7 to determine which code to call, 32bit or 64bit, it was always calling the 64bit code.
I changed it to WIN64 and now it behaves as it should (for 32bit at least)
So when should you use one or the other? My impression was VB7 was suffcient from what I have see from other code?
Code in question is ListAllOpenWindowTitles()
In attempting to help another member on another site, ChatGPT gave me code but for 64bit, and I am on 32bit Office.
I amended that code to have the conditional compile directive VBA7, and that appeared to work.
The code was to close a particular window with a known title.
I then thought about finding out the actual name of the title of the window we are loooking for, which generally is in the form Filename - Application Name.
However despite using VBA7 to determine which code to call, 32bit or 64bit, it was always calling the 64bit code.
I changed it to WIN64 and now it behaves as it should (for 32bit at least)
So when should you use one or the other? My impression was VB7 was suffcient from what I have see from other code?
Code:
Option Compare Database
Option Explicit
#If VBA7 Then
' For window closing
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, _
ByVal lParam As LongPtr) As LongPtr
' For window finding
Declare PtrSafe Function EnumWindows Lib "user32" ( _
ByVal lpEnumFunc As LongPtr, _
ByVal lParam As LongPtr) As Long
Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As LongPtr, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Declare PtrSafe Function IsWindowVisible Lib "user32" ( _
ByVal hwnd As LongPtr) As Long
#Else
' For window closing
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
' For window finding
Declare Function EnumWindows Lib "user32" ( _
ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Declare Function IsWindowVisible Lib "user32" ( _
ByVal hwnd As Long) As Long
#End If
Private Const WM_CLOSE = &H10
Sub CloseWindow(strFileName As String, strAppName As String)
#If VBA7 Then
Dim hwnd As LongPtr
#Else
Dim hwnd As Long
#End If
Dim strWindowTitle As String
strWindowTitle = strFileName & " - " & strAppName
hwnd = FindWindow(vbNullString, strWindowTitle)
If hwnd <> 0 Then
Call SendMessage(hwnd, WM_CLOSE, 0, 0)
Else
MSGBOX "Window " & strWindowTitle & " not found."
End If
End Sub
Function EnumWindowsProc64(ByVal hwnd As LongPtr, ByVal lParam As LongPtr) As Long
Dim title As String * 256
If IsWindowVisible(hwnd) Then
If GetWindowText(hwnd, title, Len(title)) > 0 Then
Debug.Print Trim(title)
End If
End If
EnumWindowsProc64 = 1 ' continue enumeration
End Function
Function EnumWindowsProc32(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim title As String * 256
If IsWindowVisible(hwnd) Then
If GetWindowText(hwnd, title, Len(title)) > 0 Then
Debug.Print Trim(title)
End If
End If
EnumWindowsProc32 = 1 ' continue enumeration
End Function
Sub ListAllOpenWindowTitles()
#If Win64 Then
EnumWindows AddressOf EnumWindowsProc64, 0
#Else
EnumWindows AddressOf EnumWindowsProc32, 0
#End If
End Sub
Code in question is ListAllOpenWindowTitles()