将Internet Explorer窗口置于前台

时间:2018-02-09 17:26:17

标签: excel vba excel-vba internet-explorer visibility

我有一个打开Internet Explorer的宏

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

稍后宏与其他窗口交互,因此IE失去焦点。

但是,在其他交互之后,我需要将密钥发送到IE应用程序。我搜索了如何再次激活IE窗口,但没有一个工作。

我试过(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 个答案:

答案 0 :(得分:4)

这更像是 hack 。基本上,你会隐藏它然后立即取消隐藏它。

你可以试试这个Sub:

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

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

End Sub

你会像这个例子一样使用它:

Sub Test()

    Dim ie As New InternetExplorer

    ' Addt'l Code

    ' IE Obj loses focus here

    ieBringToFront ie

End Sub
  

enter image description here