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

Dulce 21 Reputation points
2021-03-01T18:47:11.003+00:00

I'm working on an app to open diferent links and I want to open it on Internet explorer on a tab as many links I clicked I'm using the code belown but it works with Microsoft Edge and not with internet Exprorer. Can someone help me?

 else if (browser_box.Text == "Explorer")
            {
                //var shellWindows = new SHDocVw.ShellWindows();
                //var shellWindows = new SHDocVw.ShellWindows();
                SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
                if (shellWindows.Count > 0)
                {

                    InternetExplorer ie = shellWindows.Item(0);
                    //InternetExplorer ie = new SHDocVw.InternetExplorer();

                    openNewInternetExplorerTab(ref ie, web_link);
                }
                else
                {
                    InternetExplorer ie = openNewInternetExplorerInstance(web_link);
                }
            }

THERE ARE openNewInternetExplorerTab AND openNewInternetExplorerInstance FUNCTIONS

public static InternetExplorer openNewInternetExplorerInstance(string url)
        {
            // Create an InternetExplorer instance
            //InternetExplorer ie = new InternetExplorer();
            SHDocVw.InternetExplorer ie = new SHDocVw.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)
        public static void openNewInternetExplorerTab(ref SHDocVw.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;
        }
Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,173 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,837 questions
0 comments No comments
{count} votes

Accepted answer
  1. Deepak-MSFT 2,191 Reputation points Microsoft Vendor
    2021-03-02T06:11:11.177+00:00

    @Dulce ,
    I suggest you try to make a test with the sample code below can help you to launch the new tab in the existing IE instance.

     InternetExplorer ie = null;  
      
            SHDocVw.ShellWindows allBrowser = new SHDocVw.ShellWindows();  
            int browserCount = allBrowser.Count - 1;  
            while (browserCount >= 0)  
            {  
                ie = allBrowser.Item(browserCount) as InternetExplorer;  
                if (ie != null && ie.FullName.ToLower().Contains("iexplore.exe"))  
                {  
                    ie.Navigate2("http://localhost", 0x1000);  
                    break;  
                }  
                browserCount--;  
      
            }  
    

    Output:

    73238-226.gif

    Further, you can try to modify the code example as per your own requirements.

    ----------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful