Novick Software
SQL Server Consulting • Design • Programming • Tuning

  andy novick is a sql server mvp

 

 

Tips and Tricks for:  C#

The Problem:   Opening a Internet Explorer browser window to a specific URL

When you want the user to see a web page, you've got to open a new browser window and navigate it to the desired URL.  The examples in the documentation are all in Visual Basic 6 or VBScript.  How do you do it in .Net?


Solution: Create and use the COM object InternetExplorer.Application

This routine IEOpenURL shown below does the job.

You'll need to start by adding a reference to the shDocVw.Dll.  It shows up as AXSHDocVW in the References branch of Solution Explorer.  You'll also need the COMCreateObject function.

The IDOpenURL function is static so I usually include it in my NSLibStatic class.  The sole purpose of the class is to hold static methods that I write.  You can put it anywhere you like.

The file that holds the routine needs this using statement:

    using SHDocVw; // Contains the Internet Explorer reference

Then put this method into one of your classes:

/// <summary>
/// Opens a new Internet Explorer window and navigates it to the URL.
/// This code is for demonstration purposes only.
/// </summary>
/// <param name="sURL">URL to navigate to.</param>
/// <returns>true all the time.</returns>
public static bool IEOpenOnURL (string sURL)
{
	InternetExplorer oIE = (InternetExplorer)NSLibStatic.COMCreateObject 
                                                      ("InternetExplorer.Application");
	if (oIE != null)
	{	
		object oEmpty = String.Empty;
		object oURL= sURL;
		oIE.Visible = true;
		oIE.Navigate2 (ref oURL, ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty);
	}
	return true;			
}

 

To call it, just supply the URL (assumes the function is in the NSLibStatic class:

    NSLibStatic.IEOpenURL ("http://www.NovickSoftware.com");

 


RSS as HTML

Personal Blog

 
New Tips:

Use dsinit to set the SQL Server instance for Windows Azure dev storage

Upcoming
Presentations:

SQL PASS
Nov 7, '12
Biggest
Loser: DB
Edition


Full Schedule