Visual Basic and IE 7, open pages in new Tab or Window or wait for the page to be displayed completely


I was working on a project where I had the following requirement, and for a moment I thought, "Man, that's like a 5 min task for me!!". And it should have been!!!! Unfortunately though, I couldn't find the right document at the right time and it took me some time to achive what I wanted to achieve 🙁 


The task at my hand was...
1. Open a Page in Internet Explorer and wait for it to load completely.
2. Once the first page is loaded, Open a new Window and navigate to another Page.
3. Then, Open a new Tab in IE 7 and navigate to yet another page.


Since, I have not worked much into automation of Objects, I was looking for some ready made code, and after a little bit of struggle (which I didn't expect, honestly), I ended up with the following in VB6. Yes... VB6 🙂 (I am yet to install VS.NET on my home PC after a recent format of the box!)


Okay, so here we go...


First things first! You will need to add a Reference using the Project->References for "C:\Windows\System32\shdocvw.dll". Just drop a command button and call it cmdDoMyTask.


Private Sub cmdDoMyTask_Click()
On Error GoTo ErrHandler
    Dim objIE As SHDocVw.InternetExplorer
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True
    objIE.Navigate2 "http://www.microsoft.com/"
    'Wait till the page is completely displayed!
    'The following three lines are not needed if
    'you don't want to wait for the 1st page to display completely
    While objIE.ReadyState < 4
        Sleep 100
    Wend
    'Open in new Window (u need to pass the value 1)
    objIE.Navigate2 "http://www.live.com/", 1
    'Open in new Tab (u need to pass the value 2048)
    objIE.Navigate2 "http://www.msn.com/", 2048
    Set objIE = Nothing
    Exit Sub
ErrHandler:
    Set objIE = Nothing
End Sub


By the way, here is the document which helped me out for the "Open in new Tab"! Phew!!!


I hope that helps,
Rahul

Comments (12)

  1. Very good. Blut the real problem that arose from the moment I had to do with IE7 instead of IE6 is:

    upon exiting (closing) (= Event "OnQuit") an instance of SHDocVw.InternetExplorer its parameter "LocationURL" does no longer contain the URL natigated to, but "about:blank"!

    What am I now to do programming with VB6?

    Thanks for your answer.

    Paul

  2. Paul G says:

    Super shiney !

    I’ev been looking for exactly this since I installed IE7.

    Thanks for the good work.

  3. Sudhir says:

    Hi,

    Thanks Rahul….I noticed this does not work with VBA and on MS office on Vista. Each time the navigate method is called, a new window is opened. I want it to open in the same window.

    any help will be useful

    thanks

  4. Sudhir says:

    Hi,

    This is further to my post above: In Vista 64 bit, while running VBA-Excel, I enclose the Internetexplorer in a class and within that class I did the event handling of Ie’s events. Outside of the class I accessed the page downloaded by IE and parsed the contents. I have the following problems which I dont see while running on xp+office 2003 combo:

    1. The code outside of the class loses reference to the IE object inside the class.

    2. Navigation happens with another instance of the ie opened and the parent instance is left blank

    3. I got over the above problem by instantiating the ie object and doing the navigation together in one sub- that atleast got the navigation to happen in the same window.

    4. No events (not even) before Navigate fires

    Note: None of this happens on a 32bit XP with office 2003. Is this an issue with running office as a 32 bit application on the 64bit machine?

    thanks

    regards

    Sudhir

  5. imRahulSoni says:

    Hi Sudhir,

    I am at holidaying these days and don’t have access to a Vista-64 bit box. I will be back in office in around a week (1st July) and will look into it.

    Sorry about the delay…

    Regards,

    Rahul

  6. Sudhir says:

    Hello Rahul,

    This issue looks like is realted to marshalling data across 2 32 bit applications in a 64 bit environmnet.

    The IE is 32 bit (I confirmed that the IE evoked is a 32 bit one and not a 64 bit one). Office 2003 is 32 bit- so we have a case where data is being accessed by these 2 32 bit apps, while executing in a 64 bit environmnet.

  7. Gratefully yours, says:

    Grrrrrrrrreat!!!  Thank you so much.

    I was using "sendkeys ^T" in vba, ain’t do anything.

  8. Malar says:

    Hi,

    I have problem in navigating a page in IE7 (works perfectly in IE6).

    Here is the code on button click.  It opens a new window in IE6. In IE7, it opens a new window but URL is opening in 2nd tab. In First tab it says connecting. but loads properly in second tab. I tried setting all my tab option in Tools-options.

    can some one help me here?

              Dim browser As SHDocVw.InternetExplorer

           Dim o = Nothing

           Dim findurl = "http://www.google.com&quot;

           browser = New SHDocVw.InternetExplorer

           browser.Navigate(findurl, o, "gps_map", o, o)

  9. OBA says:

    thanks! just what i was looking for.

  10. Ankur Garg says:

    Hi,

    If you want to navigate an IE page on Vista, follow these simple step:

    1) Open Internet Explorer.

    2) Open Tools –> Internet Options –> Security Tab

    3) Unselect the protected mode at the bottom of the pane.

    4) Click OK and close the Internet Explorer.

    Try again running your program. No new window will be opened this time.

    Note : Your security may be at risk with this.

    If you find this useful, send me an email at mr.ankurgarg@rediffmail.com

    Thanks a lot.

    Ankur

  11. wasim says:

    thanks , can u tell me how to make active specific tab

    for example a window is minimize with more one than tabs. i want to make active specific tab in IE

    thanks in advance

  12. Bijayani says:

    Hi,

    Though I am not a technical person but find your post to be quite informative and impressive.

    I would like to share a link where a software engineer of my company has shared a tip on "Programmatically open new tabs in IE 7 using VB".

    Here is the link:

    http://www.mindfiresolutions.com/Programmatically-open-new-tabs-in-IE-7-using-VB-28.php

    Hope you find it useful and of assistance.

    Thanks,

    Bijayani

Skip to main content