I'm making a macro that opens Internet Explorer, navigates and logs into a website. Everything works fine, but I need to bring the IE Window up front, and activate it, so I can use SendKeys
on it. I've found websites and videos with different approaches on a command called AppActivate
and i've tried many of them, but even if I copy the entire code (which works for the author) it won't work for me, I always get an error: Invalid Procedure Call or Argument - Error 5
A list of everything I've found and tried:
Dim objIE As InternetExplorerMedium
Set objIE = New InternetExplorerMedium
objIE.navigate "http://google.com"
'makes it visible, but not active
objIE.Visible = True
'A list of ways I've tried:
objIE.AppActivate "Google - Internet Explorer"
AppActivate "Google - internet Explorer"
'the above supposedly looks for the title of the page
AppActivate objIE
AppActivate (objIE)
AppActivate "objIE"
observations: I'm running this inside Excel 2013 and I'm using Windows 7 with IE 11.
I always just make IE an untyped object, like so
Sub test()
Dim IE As Object
Set IE = Nothing
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "www.google.com"
IE.Visible = True
End Sub
The IE window has focus when this finishes running on my end.
Someone write similar things here:
With a little modification: (SetForegroundWindow Lib "user32" ) So that after it search for the Existing IE, it will appear on the top of our screen
*PtrSafe / LongPtr is to be used in 64-bit system *You may delete it if you are using 32-bit
Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal HWND As LongPtr) As LongPtr
Sub Test1()
Dim IE1 As Object
Set IE1 = ThisIE
IE1.navigate "http://stackoverflow.com"
Do While IE1.readyState <> READYSTATE_COMPLETE
Loop
SetForegroundWindow (IE1.HWND)
End Sub
Function ThisIE() As Object
For Each ThisIE In CreateObject("Shell.Application").Windows()
If (Not ThisIE Is Nothing) And ThisIE.Name = "Internet Explorer" Then Exit For
Next ThisIE
If ThisIE Is Nothing Then Set ThisIE = CreateObject("InternetExplorer.Application")
ThisIE.Visible = True
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.