shrivallabha
09-08-2012, 05:31 AM
Hi All,
This code is annoying me a little. The following bit of code is put in a standard module.
1) Objective of this code: To bring window to front based on the partial header match e.g. suppose I've "Some User IBM Lotus Notes" running and I use "IBM Lotus Notes" then it should bring the window to the front. [At this point I'd not like to bother about multiple windows having same text as it is not happening at the moment.]
2] The code so far:
In usage:
Public Sub TestTheCode()
'This is how we can use the code in our program
Window_Activate "VBA Express"
End Sub
And the Windows_Activate code:
Option Explicit
'This function is used to get into the loop
Declare Function GetActiveWindow Lib "user32" () As Long
'This function gets the length of window caption text
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" _
(ByVal hwnd As Long) As Long
'This function loads up lpstring part with Window Text followed by null character
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'This function is used for looping through windows
Declare Function GetNextWindow Lib "user32" Alias "GetWindow" _
(ByVal hwnd As Long, ByVal wFlag As Long) As Long
'This function brings the desired window to top
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Public Sub Window_Activate(strWinText As String)
Dim lngName As Long, lngLength As Long, lngWindow As Long
Dim strWindow As String
lngName = GetActiveWindow
Do
strWindow = Space$(512)
lngLength = GetWindowTextLength(lngName)
lngWindow = GetWindowText(lngName, strWindow, lngLength + 1)
strWindow = Left(strWindow, lngLength)
If InStr(strWindow, strWinText) > 0 Then
'AppActivate lngName :works for Lotus Notes
ShowWindow lngName, 3 'Maximizes the window
BringWindowToTop lngName 'Bring the window to top
Exit Do
End If
lngName = GetNextWindow(lngName, 2)
Loop While lngName > 0
End Sub
3] Problem : When a window [which is to be activated] is minimized then both window to be brought to front and Excel lose "focus" and remain hung. As soon as you click on window to be activated things work fine. The code as it is runs without hitch when the window to be brought front is maximized or non-minimized.
I hope to get some way forward through this. Thank you.
This code is annoying me a little. The following bit of code is put in a standard module.
1) Objective of this code: To bring window to front based on the partial header match e.g. suppose I've "Some User IBM Lotus Notes" running and I use "IBM Lotus Notes" then it should bring the window to the front. [At this point I'd not like to bother about multiple windows having same text as it is not happening at the moment.]
2] The code so far:
In usage:
Public Sub TestTheCode()
'This is how we can use the code in our program
Window_Activate "VBA Express"
End Sub
And the Windows_Activate code:
Option Explicit
'This function is used to get into the loop
Declare Function GetActiveWindow Lib "user32" () As Long
'This function gets the length of window caption text
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" _
(ByVal hwnd As Long) As Long
'This function loads up lpstring part with Window Text followed by null character
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'This function is used for looping through windows
Declare Function GetNextWindow Lib "user32" Alias "GetWindow" _
(ByVal hwnd As Long, ByVal wFlag As Long) As Long
'This function brings the desired window to top
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Public Sub Window_Activate(strWinText As String)
Dim lngName As Long, lngLength As Long, lngWindow As Long
Dim strWindow As String
lngName = GetActiveWindow
Do
strWindow = Space$(512)
lngLength = GetWindowTextLength(lngName)
lngWindow = GetWindowText(lngName, strWindow, lngLength + 1)
strWindow = Left(strWindow, lngLength)
If InStr(strWindow, strWinText) > 0 Then
'AppActivate lngName :works for Lotus Notes
ShowWindow lngName, 3 'Maximizes the window
BringWindowToTop lngName 'Bring the window to top
Exit Do
End If
lngName = GetNextWindow(lngName, 2)
Loop While lngName > 0
End Sub
3] Problem : When a window [which is to be activated] is minimized then both window to be brought to front and Excel lose "focus" and remain hung. As soon as you click on window to be activated things work fine. The code as it is runs without hitch when the window to be brought front is maximized or non-minimized.
I hope to get some way forward through this. Thank you.