Activatewindow, Activate External Window

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,066
I use the following script to activate various windows, however sometimes I receive and error message saying "can not bring window to foreground", I have tried using various on error scripts however nothing bypasses it.

Does anyone have a solution.

Example of the scripts that I use to activate windows.
I activate more than just internet Exlorer, it is just an example.

Code:
Activatewindow "Internet Explorer"

Code:
Option Private Module
Private Declare Function FindWindowA _
   Lib "User32.dll" _
     (ByVal lpszClass As String, _
      ByVal lpszWindow As String) As Long
 
'Checks if the window is minimized to the TaskBar
 Private Declare Function IsIconic _
   Lib "User32.dll" _
     (ByVal hWnd As Long) As Long
 
'Returns the handle to the window currently receiving input
 Private Declare Function GetForegroundWindow _
   Lib "User32.dll" () As Long
'Directs User Input to the specified Window
'Success gives a Non Zero Return Value
 Public Declare Function SetForegroundWindow _
  Lib "User32.dll" _
   (ByVal hWnd As Long) As Long
 
'Bring Window to the Top of the Z-Order
 Private Declare Function BringWindowToTop _
  Lib "User32.dll" _
   (ByVal hWnd As Long) As Long
 Private Declare Function AttachThreadInput _
   Lib "User32.dll" _
     (ByVal idAttach As Long, _
      ByVal idAttachTo As Long, _
      ByVal fAttach As Long) As Long
 
'The return value is the identifier of the thread that created the window.
 Private Declare Function GetWindowThreadProcessId _
   Lib "User32.dll" _
     (ByVal hWnd As Long, _
      ByRef lpdwProcessID As Long) As Long
'Function to Change how Window is Displayed
 Private Declare Function ShowWindow _
  Lib "User32.dll" _
   (ByVal hWnd As Long, _
    ByVal nCmdShow As Long) As Long
 
'Constants for ShowWindow (nCmdShow)
 Const SW_HIDDEN As Long = 0
 Const SW_NORMAL As Long = 1
 Const SW_MINIMIZED As Long = 2
 Const SW_MAXIMIZED As Long = 3
 Const SW_NOTACTIVE As Long = 4
 Const SW_UNHIDDEN As Long = 5
 Const SW_MINWITHFOCUS As Long = 6
 Const SW_MINNOTACTIVE As Long = 7
 Const SW_RESTORE As Long = 9
 Private Declare Function GetWindow _
   Lib "User32.dll" _
     (ByVal hWnd As Long, _
      ByVal wCmd As Long) As Long
 Private Declare Function GetWindowText _
   Lib "User32.dll" _
     Alias "GetWindowTextA" _
       (ByVal hWnd As Long, _
        ByVal lpString As String, _
        ByVal nMaxCount As Long) As Long
 Private Declare Function GetDesktopWindow _
   Lib "User32.dll" () As Long
Function GetHwnd(Window_Title As String) As Long
  Dim hWnd As Long
  Dim RetVal As Long
  Dim Title As String
 
  Const GW_HWNDNEXT As Long = 2
  Const GW_CHILD As Long = 5
 
    hWnd = GetWindow(GetDesktopWindow, GW_CHILD)
 
    While hWnd
      Title = String(512, Chr$(0))
      RetVal = GetWindowText(hWnd, Title, Len(Title))
        If LCase(Title) Like "*" & LCase(Window_Title) & "*" Then
           GetHwnd = hWnd: Exit Function
        End If
      hWnd = GetWindow(hWnd, GW_HWNDNEXT)
    Wend
End Function
Sub ActivateWindow(ByVal Window_Title As String)
  Dim AppHwnd As Long
  Dim NewPID As Long
  Dim NewAppThreadID As Long
  Dim RetVal As Long
  Dim ThisPID As Long
  Dim ThisAppThreadID As Long
  Dim WndTitle As String
 
    'Build the Window Title
     WndTitle = Window_Title
 
    'Get a handle on the Window you want to send the mouse clicks to
     AppHwnd = GetHwnd(WndTitle)
 
    'Was window found?
     If AppHwnd <> 0 Then
       'Application window found by the caption
        ThisAppThreadID = GetWindowThreadProcessId(GetForegroundWindow, ThisPID)
        NewAppThreadID = GetWindowThreadProcessId(AppHwnd, NewPID)
 
       'Attach the other Application's thread input to this one
        Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, True)
 
       'Make the other Application the top window
        RetVal = SetForegroundWindow(AppHwnd)
 
       'Detach the other Application's thread input
        Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, False)
 
          If RetVal <> 0 Then
            'Maximize the window if it's been minimized
            'or just show it if it's already opened.
             If IsIconic(AppHwnd) Then
               Call ShowWindow(AppHwnd, SW_MAXIMIZED)
             'Else
               'Call ShowWindow(AppHwnd, SW_RESTORE)
             End If
          Else
            MsgBox "Can't Bring Window to the Foreground."
          End If
     Else
       'Failed to find the window caption
       '(the app is probably closed or the wrong window name is passed)
        MsgBox "Application Window '" & WndTitle & "' Not Found."
     End If
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,155
Messages
6,170,403
Members
452,325
Latest member
BlahQz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top