• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Bring Specific Window To Top by specifying its Window-Name

shrivallabha

Excel Ninja
Sometimes we need to bring a specific window to front (activate) but we do not know its complete name. Following API based code can do that. The usage of module can be changed by the user after understanding the code. Feel free to do so.


PS: I've kept this as KB entry (For Approval) on VBAX. Sooner or later the entry will appear there as well.


Implementation:

1. Go to visual basic editor and do Insert | Module

2. Assign a suitable name for the module e.g. GetWinAtTop

2. Now paste the following code in this module.

[pre]
Code:
Option Explicit
' ShowWindow() Commands
Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Const SW_MAX = 10

'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, SW_SHOW
ShowWindow lngName, SW_SHOWMAXIMIZED 'Maximizes the window
BringWindowToTop lngName 'Bring the window to top
Exit Do
End If

lngName = GetNextWindow(lngName, 2)

Loop While lngName > 0

End Sub
4. This completes implementation part.

Usage:

In your code you can use it by calling the "Window_Activate" with necessary name arguments like:

Public Sub TestTheCode()
'This is how we can use the code in our program
Window_Activate "The vault"
End Sub
[/pre]
 
Back
Top