3

I have a macro that opens the Internet Explorer

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

Later the macro interacts with other windows, so the IE loses the focus.

But, after the other interactions, I need to send keys to the IE application. I searched how to activate again the IE Window, but none worked.

I tried (1)

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Sub test()
  Set acObj = GetObject(, "InternetExplorer.Application")
  SetForegroundWindow acObj.hWndAccessApp
End Sub

(2)

Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Sub test()
  Dim IE As Object
  Set IE = CreateObject("InternetExplorer.Application")
  'code
  SetForegroundWindow IE.hWndAccessApp
End Sub

(3)

IE.Focus 'or IE.Document.Focus

(4)

AppActivate("exactly_name_of_the_window")
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
Guilherme
  • 161
  • 3
  • 14
  • https://www.experts-exchange.com/questions/24141365/How-do-I-activate-bring-to-front-an-internet-explorer-window-in-VBScript.html – braX Feb 09 '18 at 17:51

1 Answers1

7

This is more of a hack than anything. Basically, you will hide it then immediately unhide it.

You could try this Sub:

Sub ieBringToFront(ieObj As InternetExplorer) ' or (ieObj As Object) --> Late Binding

    With ieObj
        .Visible = False
        .Visible = True
    End With

End Sub

You would use it like this example:

Sub Test()

    Dim ie As New InternetExplorer

    ' Addt'l Code

    ' IE Obj loses focus here

    ieBringToFront ie

End Sub

enter image description here

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • 1
    This is so simple... Yet so brilliant. –  Feb 10 '18 at 11:11
  • @K.Davis That is simple and a good idea. But, i tried it today and it didn't work. I think it's because i'm calling the macro from a batch vbs file (i need to do this because i use the task manager on windows to run the macro daily through the vbs file). When i run the macro directly, it did work. Don't know how to fix it – Guilherme Feb 15 '18 at 13:56