0

I am not able to understand what is the problem with following code -

i = 15
While (i < 100)
    If i Mod 2 = 0 Then
        handle = FindWindow(vbNullString, "My Details - Windows Internet Explorer")
        Range("A1").Value = handle
        BringWindowToTop handle
        Application.Wait DateAdd("s", 1, Now)
    Else
        handle1 = FindWindow(vbNullString, "Codeomnitrix - Outlook Web App - Mozilla Firefox")
        Range("A2").Value = handle1
        BringWindowToTop handle1
        Application.Wait DateAdd("s", 1, Now)
    End If

    i = i + 15
Wend

It should switch between both the windows and let them be in focus till 1 second but what actually happens is it just put Firefox on top and then no switching.

Thanks

codeomnitrix
  • 4,179
  • 19
  • 66
  • 102
  • Does `FindWindow(vbNullString, "Codeomnitrix - Outlook Web App - Mozilla Firefox")` return zero or a value? For example I tested with Excel: **"test.xlsm - Microsoft Excel"** the `Findwindow()` has to look for **"test.xlsm"** instead for it to work. – Raybarg Jul 18 '13 at 06:50
  • No Raybarg it is returning proper values for both the windows, Also if i run on thing at a time it works properly. I mean if only IE window needs to bring to front or only Firefox window needs to bring front it works fine but switching not working – codeomnitrix Jul 18 '13 at 07:41
  • You should `Debug.Print` inside the `If()` blocks the value of **i** and the **handle**, see if it runs trough the whole loop. I tested your code with File Explorer and Excel and it works fine. Could be that firefox catches the API hook, maybe some anti-popup thing in it? I am just guessing... – Raybarg Jul 18 '13 at 07:47

2 Answers2

1

If you consider using Autoit this can be very simple.

Adding reference to autoit dll.

enter image description here

Download Autoit

Sub test()

   Dim oAutoit As New AutoItX3
   oAutoit.Opt "WinTitleMatchMode", 2  ' Match any substring in the title
   oAutoit.WinActivate "My Details - Windows Internet Explorer" ' Activates (gives focus to) a window.

End Sub
Santosh
  • 12,175
  • 4
  • 41
  • 72
0

I found out that minimized window will not pop up when activated.

Private Declare Function GetWindowPlacement Lib _
        "user32" (ByVal hwnd As Integer, _
        ByRef lpwndpl As WINDOWPLACEMENT) As Integer
Private Declare Function SetWindowPlacement Lib "user32" _
       (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

Private Const SW_SHOWMINIMIZED As Short = 2
Private Const SW_SHOWMAXIMIZED As Short = 3
Private Const SW_SHOWNORMAL As Short = 1

Private Structure WINDOWPLACEMENT
    Dim length As Integer
    Dim flags As Integer
    Dim showCmd As Integer
    Dim ptMinPosition As POINTAPI
    Dim ptMaxPosition As POINTAPI
    Dim rcNormalPosition As RECT
End Structure

[...]

Dim handle As Integer
Dim wp As WINDOWPLACEMENT

handle = FindWindow(vbNullString, "My Details - Windows Internet Explorer")
GetWindowPlacement(handle, wp)
wp.showCmd = SW_SHOWNORMAL
SetWindowPlacement(handle, wp)

All this copied from Codeproject article to restore the window.

Raybarg
  • 730
  • 6
  • 12