Bringing Internet Explorer window to foreground

断了今生、忘了曾经 提交于 2020-07-18 07:15:27

问题


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")

回答1:


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



来源:https://stackoverflow.com/questions/48711116/bringing-internet-explorer-window-to-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!