Reputation: 31
I have a script where I create an IE window through CreateObject("InternetExplorer.Application")
. The problem is, whenever I run this script, it always opens behind whatever else might already be open on my machine. I want this IE window to open on TOP of everything else. It does not have to be "always on top", like the option in Task Manager, but it should at least initially open on top. After that, I don't care what happens. I have searched high and low and have been unable to find a way to achieve this. I have tried appactivate
and focus()
but neither of those seem to work. Any suggestions?
I am running Windows 7 with IE 11
Upvotes: 2
Views: 20603
Reputation: 1
You can use SetWindowPos API function to bring any window you want to front by its window handle. The InternetExplorer object already has a property to call the handle(.Application.hwnd):
Public Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Sub Test()
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
SetWindowPos ie.hwnd, 0, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
End Sub
Upvotes: 0
Reputation: 151
I found the sequence affects the behavior. Don't make IE visible until after it has finished loading.
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.google.com"
While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
ie.visible = True
DoEvents
Upvotes: 1
Reputation: 177
I messed around with a bunch of the solutions on the internet but eventually found the easiest one that worked.
Set objExplorer = CreateObject ("InternetExplorer.Application")
objExplorer.document.focus()
Upvotes: -1
Reputation: 637
You probably have the problem because the title of the IE window is not exactly the title of the page (ie. "Yahoo - Internet Explorer") Therefore you must bring it to the front before you start navigating to the page :
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
CreateObject("WScript.Shell").AppActivate "Internet Explorer"
ie.Navigate "http://www.yahoo.com/"
Upvotes: 7