question

Dulce-7957 avatar image
0 Votes"
Dulce-7957 asked Deepak-MSFT answered

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

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;
         }


ms-edgemicrosoft-graph-explorer
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Deepak-MSFT avatar image
1 Vote"
Deepak-MSFT answered

@Dulce-7957,
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.





226.gif (272.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.