How to force Excel VBA to open new window with IE in front

I have the following VBA code in Excel 2013 that most of the time opens IE in front of the Excel VBA executing and sometimes behind it. When IE opens behind Excel the VBA cannot operate on IE. I receive an automation error having to restart the script and manually having to click on the IE window just in time as it is created to force it to the front.

How can I enforce always opening the new IE window in front of the Excel VBA?

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.Navigate "http://www.google.com/"
JenproAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Use this way...

 
Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
     
    While IE.Busy
        DoEvents
    Wend
     
    With IE
        .Visible = True
        .navigate "https://www.google.co.in/"
    End With
    

Open in new window


This will wait for your code till the time IE is fully ready...

Saurabh...
JenproAuthor Commented:
Saurabh,

Thanks for the suggestion but my issue is not navigation, it is the window created with IE sometime opens behind Excel and not in front. How can I ensure the window with IE always opens in front of Excel executing the VBA code?
Saurabh Singh TeotiaCommented:
How would it make a difference if it's in front or behind??
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

JenproAuthor Commented:
When IE opens in a window behind Excel the VBA does not execute and I receive an automation error. When it opens in the front VBA executes normal. I am unable to determine what makes IE open in the back and sometimes in the front for no apparent reason. I need a way to force it to always open in the front.
Saurabh Singh TeotiaCommented:
Can you do the necessary changes which i told you and then post back for any issues..
JenproAuthor Commented:
I set it to exact code you have and just ran another test. IE window still opens in the back, behind Excel running the VBA code. Can you suggest how to make IE always open in front of Excel?
Saurabh Singh TeotiaCommented:
Okay try using this...

Also in order to use it go to tools-->references-->ADD-->Microsoft Internet controls and run this code..

Dim IE
    Set IE = New InternetExplorer
     
    While IE.Busy
        DoEvents
    Wend
     
    With IE
        .Visible = True
        .navigate "https://www.google.co.in/"
    End With

Open in new window

JenproAuthor Commented:
Saurabh,

Microsoft Internet Controls have been checked. The code is working as you have it, however the window with IE sometimes opens in front of Excel and sometime behind it.  Is using SetForegroundWindow an option?
Saurabh Singh TeotiaCommented:
If you run the code by f5 by default it will come over on the excel..as in front not behind..However if you do it by f8 it will come in front for one line however as soon you go to next line it will be behind..so just run the code by pressing f5 not by f8..and another way u can use it by this...

  Dim objIEBrowser
  
    Set objIEBrowser = CreateObject("InternetExplorer.Application")
     objIEBrowser.Visible = True
       objIEBrowser.Navigate2 "www.google.com"
           Do While objIEBrowser.Busy

Loop

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JenproAuthor Commented:
Thanks Saurabh. The F5 execution seems to have worked. Prior I was executing the macro from an icon in Excel's quick access toolbar. Would you know if there is a way to still execute it from there with the desired outcome, i.e. opening IE window in the front?
Saurabh Singh TeotiaCommented:
I don't think so you can do that normally i do testing as in when i press f8 i go back to internet explorer..and then continue pressing F8 and it works like normally...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.