Gui*_*rme 2 excel internet-explorer vba visibility excel-vba
我有一个打开Internet Explorer的宏
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
Run Code Online (Sandbox Code Playgroud)
后来,宏与其他窗口进行交互,因此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
Run Code Online (Sandbox Code Playgroud)
(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
Run Code Online (Sandbox Code Playgroud)
(3)
IE.Focus 'or IE.Document.Focus
Run Code Online (Sandbox Code Playgroud)
(4)
AppActivate("exactly_name_of_the_window")
Run Code Online (Sandbox Code Playgroud)
这比任何东西更像是一个hack。基本上,您将其隐藏然后立即取消隐藏。
您可以尝试以下Sub:
Sub ieBringToFront(ieObj As InternetExplorer) ' or (ieObj As Object) --> Late Binding
With ieObj
.Visible = False
.Visible = True
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
您可以像以下示例一样使用它:
Sub Test()
Dim ie As New InternetExplorer
' Addt'l Code
' IE Obj loses focus here
ieBringToFront ie
End Sub
Run Code Online (Sandbox Code Playgroud)