Multiple Apps

Bring Specific Window To Top by specifying it Window-Name

Ease of Use

Intermediate

Version tested with

2007 

Submitted by:

shrivallabha

Description:

This is Win 32 API based code which brings specified Window-Name to Top. 

Discussion:

When you are dealing with multiple applications you'd want to bring the other application window to the top e.g. Bringing a Lotus Notes window to the top by using Excel. There's AppActivate in VBA but it has its own limitation. So if you wanted to bring "the thing" to top, how would you do it? This is one way of doing it. I hope you like it. Feel free to change / modify it to suit your requirements. Generally, you should specify the complete window name but in most of the cases it will work with partial matches. Only risk being, the first match will be brought to front. 

Code:

instructions for use

			

Option Explicit 'Thanks to Tommy of VBAExpress! ' 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

How to use:

  1. 1. Insert a module using Insert | Module in the VBE window
  2. 2. Paste this code in coding pane. And you're done!
  3. PS: If you don't know how to open VBE window then in Excel press CTRL + F11 simultaneously.
 

Test the code:

  1. In your code use it like:
  2. Window_Activate "VBA Express : Submission - Mozilla Firefox"
 

Sample File:

Activate Desired Application Window.zip 14.68KB 

Approved by Jacob Hilderbrand


This entry has been viewed 6 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express