Link to home
Start Free TrialLog in
Avatar of Laszlo Benedek
Laszlo BenedekFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SetForegroundWindow can't reliably bring a window to a front in excel VBA.

The code below opens Google Chrome

When I run it with Google Chrome already open, it adds an extra tab and it works as expected.

If Chrome is not yet open when I run it, it opens Chrome (as expected) however then the SetForegroundWindow line doesn't work, it doesn't bring google to the front.

(Google Chrome needs to be installed on your computer for it to work and the program uses windows location, ie you'd need to edit the folder "chromePath" for Mac and Linux)

'findwindow
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Const CB_FINDSTRINGEXACT = &H158
Const CB_SETCURSEL = &H14E
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const CB_GETCURSEL As Long = &H147
Private Const CB_GETLBTEXT As Long = &H148
Private Const CB_INSERTSTRING As Long = &H14A

'setforeground window
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

'getactivewindow
Public Declare Function GetActiveWindow Lib "user32" () As Long






Function IsFile(ByVal fName As String) As Boolean
'Returns TRUE if the provided name points to an existing file.
'Returns FALSE if not existing, or if it's a folder
    On Error Resume Next
    IsFile = ((GetAttr(fName) And vbDirectory) <> vbDirectory)
End Function


Sub GoogleTest()
  Dim chromePath As String
  Dim b As Boolean 'typical boolean for various testing
  chromePath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe""" 'path for google is same in W10 and W7
   b = IsFile(Replace(chromePath, """", ""))
  If b = False Then
  MsgBox "Google chrome cannot be found on the computer. The macro will stop."
  End If
  
  

  Shell (chromePath & " -url https://translate.google.com/")
  Dim hWnd, hWnd2, hWndExcel As Long   'windows handle for API (it's like an ID for the window)
  
  Dim i, j, k As Integer 'for control variables
  
  'measuring time variables
    Dim StartTime As Double
    Dim SecondsElapsed As Double
  
  
  StartTime = Now
  For i = 1 To 10000000 'this loop takes like 220 s on the gaming computer
    'find how much time elapsed already
    SecondsElapsed = Now
      SecondsElapsed = SecondsElapsed - StartTime
      SecondsElapsed = Round(SecondsElapsed * 3600 * 24, 0) 'now is only accurate to second
    If SecondsElapsed > 30 Then 'wait for google 30 s before error message
      MsgBox "Google couldn't be opened. Please open yourself and go to google translate otherwise the macro will stop."
      hWnd = FindWindow(vbNullString, "Google Translate - Google Chrome")
      If hWnd = 0 Then
      Exit Sub
      Else
      Exit For
      End If
    End If
    
    hWnd = FindWindow(vbNullString, "Google Translate - Google Chrome")
    If hWnd <> 0 Then Exit For
  Next
  
  


SetForegroundWindow (hWnd)


Application.Wait (Now() + 0.0000157)
Application.Wait (Now() + 0.0000157)
Application.Wait (Now() + 0.0000157)
Application.Wait (Now() + 0.0000157)
Application.Wait (Now() + 0.0000157)

MsgBox hWnd   'this checks that the handle was indeed found, so there's no error


SetForegroundWindow (hWnd)


'AppActivate "Microsoft Excel"
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Laszlo Benedek

ASKER

Thatnks that was really informative. I learned a lot :)