How to open a new tab in an existing Internet Explorer instance in C#

A problem that I recently came across is when opening a new Internet Explorer instance in C# the user was being required to login to a site that they had already logged in to. It was pretty clear that this was because the existing session was not being used as a new IE process was being started, therefore I surmised that the solution was to utilise the existing process.

I had read previously about the Navigate2 API and thought it may help but it wasn’t entirely clear how to use it and specifically how to use it on an existing IE instance, so I feel this blog may help others.

To achieve this please follow these steps:

  1. Add a reference to SHDocVW:
    1. In your Project, right click References and select Add Reference…
    2. Click COM
    3. Perform a search for Microsoft Internet Controls (or otherwise ShDocVW.dll)
    4. Select Microsoft Internet Controls and tick the box, then click OK
  2. Add the following using declaration:
    using SHDocVw;
  3. Declare the following enum and variable:
    private enum BrowserNavConstants {
    	/// <summary>
    	/// Open the resource or file in a new window.
    	/// </summary>
    	navOpenInNewWindow = 0x1,
    
    	/// <summary>
    	/// Do not add the resource or file to the history list. The new page replaces the current page in the list.
    	/// </summary>
    	navNoHistory = 0x2,
    
    	/// <summary>
    	/// Do not consult the Internet cache; retrieve the resource from the origin server (implies BINDF_PRAGMA_NO_CACHE and BINDF_RESYNCHRONIZE).
    	/// </summary>
    	navNoReadFromCache = 0x4,
    
    	/// <summary>
    	/// Do not add the downloaded resource to the Internet cache. See BINDF_NOWRITECACHE.
    	/// </summary>
    	navNoWriteToCache = 0x8,
    
    	/// <summary>
    	/// If the navigation fails, the autosearch functionality attempts to navigate common root domains (.com, .edu, and so on). If this also fails, the URL is passed to a search engine.
    	/// </summary>
    	navAllowAutosearch = 0x10,
    
    	/// <summary>
    	/// Causes the current Explorer Bar to navigate to the given item, if possible.
    	/// </summary>
    	navBrowserBar = 0x20,
    
    	/// <summary>
    	/// Microsoft Internet Explorer 6 for Microsoft Windows XP Service Pack 2 (SP2) and later. If the navigation fails when a hyperlink is being followed, this constant specifies that the resource should then be bound to the moniker using the BINDF_HYPERLINK flag.
    	/// </summary>
    	navHyperlink = 0x40,
    
    	/// <summary>
    	/// Internet Explorer 6 for Windows XP SP2 and later. Force the URL into the restricted zone.
    	/// </summary>
    	navEnforceRestricted = 0x80,
    
    	/// <summary>
    	/// Internet Explorer 6 for Windows XP SP2 and later. Use the default Popup Manager to block pop-up windows.
    	/// </summary>
    	navNewWindowsManaged = 0x0100,
    
    	/// <summary>
    	/// Internet Explorer 6 for Windows XP SP2 and later. Block files that normally trigger a file download dialog box.
    	/// </summary>
    	navUntrustedForDownload = 0x0200,
    
    	/// <summary>
    	/// Internet Explorer 6 for Windows XP SP2 and later. Prompt for the installation of Microsoft ActiveX controls.
    	/// </summary>
    	navTrustedForActiveX = 0x0400,
    
    	/// <summary>
    	/// Windows Internet Explorer 7. Open the resource or file in a new tab. Allow the destination window to come to the foreground, if necessary.
    	/// </summary>
    	navOpenInNewTab = 0x0800,
    
    	/// <summary>
    	/// Internet Explorer 7. Open the resource or file in a new background tab; the currently active window and/or tab remains open on top.
    	/// </summary>
    	navOpenInBackgroundTab = 0x1000,
    
    	/// <summary>
    	/// Internet Explorer 7. Maintain state for dynamic navigation based on the filter string entered in the search band text box (wordwheel). Restore the wordwheel text when the navigation completes.
    	/// </summary>
    	navKeepWordWheelText = 0x2000
    }
    
    private static object m = Type.Missing;

    NOTE: I took the specific enum code from somewhere because I needed better descriptions but unfortunately didn’t make a note of where (I think it was comments in an MS site) so can’t reference it. If anyone finds out please let me know so that I can reference.

  4. Create the following functions, these will allow you to create a new instance of Internet Explorer and open the given URL or open a new tab with the given URL in the given Internet Explorer instance:
    public static InternetExplorer openNewInternetExplorerInstance(string url) {
    	// Create an InternetExplorer instance
    	InternetExplorer ie = new InternetExplorer();
    
    	// Open the URL
    	ie.Navigate2(url, ref m, ref m, ref m, ref m);
    
    	// Make InternetExplorer visible
    	ie.Visible = true;
    
    	return ie;
    }
    
    public static void openNewInternetExplorerTab(ref InternetExplorer ie, string url) {
    	// Open the URL in a new tab of the given InternetExplorer instance
    	ie.Navigate2(url, BrowserNavConstants.navOpenInNewTab, ref m, ref m, ref m);
    
    	// Make InternetExplorer visible
    	ie.Visible = true;
    }
  5. And finally make use of the functions like this:
    // Create the Url
    string Url = "http://www.thepicketts.org";
    
    // Get all (if any) InternetExplorer instances
    var shellWindows = new SHDocVw.ShellWindows();
    
    // If there were any found
    if (shellWindows.Count > 0) {
    	// Get the first InternetExplorer instance found
    	InternetExplorer ie = shellWindows.Item(0);
    
    	// Open a new tab in the located InternetExplorer and navigate to the URL
    	openNewInternetExplorerTab(ref ie, Url);
    } else {
    	// Open a new InternetExplorer instance and navigate to the URL
    	InternetExplorer ie = openNewInternetExplorerInstance(Url);
    }

    This will first search for any existing Internet Explorer processes and if it finds one will open a new tab inside it, otherwise it will just create a new Internet Explorer process.

As usual, I put this information here in the hope that others will get some use out of it and so I hope this proves useful to someone. If so please do let me know as it’s nice to have feedback and of course if you do find any of the ads useful remember that I have to somehow pay to host my site 🙂

About Stephen Pickett


Stephen Pickett is a programmer, IT strategist, project manager, RightNow and telephony expert, information security specialist, all-round geek. He is currently Professional Services Director at Connect Assist, a social business that helps charities and public services improve quality, efficiency and customer engagement through the provision of helpline services and CRM systems.

Stephen is based in south Wales and attended Cardiff University to study Computer Science, in which he achieved a 2:1 grading. He has previously worked for Think Consulting Solutions, the leading voice on not-for-profit fundraising, Fujitsu Services and Sony Manufacturing UK as a software developer.

Stephen is the developer of ThinkTwit, a WordPress plugin that allows you to display multiple Twitter feeds within a blog.

9 thoughts on “How to open a new tab in an existing Internet Explorer instance in C#

  1. Hello , congratulations on the publication. I have a serious problem ! I have a client from the central service area , all computers of operators use the Chrome browser. I have an application that is integrated with SalesForce CRM , whenever you enter a connection on the computer, my application opens a tab with the customer information , the problem is that always opens thousands of tabs in the browser , that impacts the quality of care. The problem that I can not open the site within an existing tab , or can not use only one tab to do this operation , the Internet Explore can do quietly , but I’m finding it impossible to do this procedure in chrome . My problem is getting worse , you have a tip to indicate ? This issue is getting very serious to min , I will be eternally grateful if you can help me . Thanks for listening

  2. Hi Welton,

    Thanks for your kind feedback. My only suggestions are either to (if possible) detect when the tab is opened and then cease/kill the process or otherwise enforce use of Internet Explorer – not ideal, but should give you a workaround. Sorry I can’t give any better suggestions, it’s a while now since I wrote this article.

    Steve

Leave a Reply