HOW TO: Automate Internet Explorer Within a Contained Visual Basic .NET UserControl (311295)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q311295
For a Microsoft Visual Basic 6.0 version of this article, see 181075.

IN THIS TASK

SUMMARY

This article contains a Microsoft Visual Basic .NET sample file that demonstrates how to do the following:
  • Access Microsoft Internet Explorer's automation model and scripting model from within a contained Visual Basic .NET UserControl.Enumerate all controls, links, and other tags in a page and subpages. This includes an algorithm that allows you to recursively enumerate floating frames and framesets.
The following file is available for download from the Microsoft Download Center:
DownloadDownload the VBNetCtrl.exe package now. Release Date: March 5, 2002

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. back to the top

How to Run the Sample

After you download the file, run the executable file (.exe) to extract the sample files. The .NET Framework must be installed on your computer to test the sample. The UserControl is embedded in an HTML document and accesses the HTML Document Object Model. It requires Full Trust permission to run. There are two ways to give the control Full Trust permission by using the .NET Framework Configuration Wizard (Mscorcfg.msc). One way is to set up the code group of the client computer and grant it Full Trust permissions for the site in which the control resides.

For example, suppose you are running the sample from a computer named ServerName on a local intranet. To set the security permissions in that scenario, follow these steps:
  1. Start the .NET Framework configuration tool, Mscorcfg.msc.
  2. Expand each element in the following path:

    My Computer/Runtime Security Policy/Machine/Code Groups/All Code/LocalIntranet_Zone

    Right-click the LocalIntranet_Zone node, and then click new.
  3. Name the new code group TestVBNetCtrl, and then click Next to display the Choose a condition type dialog box.
  4. Select URL as the condition type, and then type http://ServerName/* in the URL box.
  5. Click Next to proceed to the Assign a Permission Set to the Code Group dialog box.
  6. Click Use existing permission set, click FullTrust, and then click Next to finish.
Alternatively, you can set the level of trust for the Assembly to Full Trust. To do this, follow these steps:
  1. Start Mscorcfg.msc.
  2. Expand each element in the My Computer/Runtime Security Policy path.
  3. Right-click the Runtime Security Policy node, and then click Trust Assembly to open the Trust an Assembly dialog box.
  4. Click Make changes to this computer, and then proceed to the next dialog box.
  5. Type the full path of the Assembly (for example, http://ServerName/VBNetCtrl.dll) in the appropriate field, or browse to the Assembly.
  6. Click Next to open the Choose the Minimum Level of Trust for this Assembly dialog box.
  7. Adjust the trust level to Full Trust, and then click Next to finish.
Finally, copy all the files in the Bin directory to any location under http://ServerName, and then browse to http://ServerName/testautoie.htm.

back to the top

Technical Notes

  • Make sure to add a reference to the Microsoft HTML Object Library (MSHTML.dll) in your project. To do this, click Add Reference on the Project menu, and then on the .NET tab, click Microsoft.mshtml.

    Note All post-Beta 2 releases of the .NET Framework SDK now include a primary interop assembly for Mshtml.dll. However, any organization that distributes the .NET Framework through the .NET Framework Redistributable must not depend on the existence of this interop assembly on clients. The assembly, named Microsoft.Mshtml.dll, can be found in the following location

    %INSTALL_DIR%\Microsoft.NET\Primary Interop

    where %INSTALL_DIR% is the installation directory chosen for the .NET Framework. You can reference this from your projects and deploy your application on any system on which the .NET Framework SDK is pre-installed.
  • Because this is an unmanaged COM component that allows only full-trusted callers, you will need Full Trust code access permissions to run the control, as discussed in the "How to Run the Sample" section of this article.
  • .NET UserControl instances cannot directly access the Document Object Model of their hosting page, as ActiveX controls do. To work around this issue, you need to expose a property in your UserControl that retrieves the HTML document from the page, as demonstrated by the following sample code:
    Private DocVal As mshtml.HTMLDocument
    
    Public Property Doc() As mshtml.HTMLDocument
        Get 
            Return DocVal
        End Get
        Set(ByVal Value As mshtml.HTMLDocument) 
            DocVal = Value
        End Set
    End Property
    					
  • The following is the syntax to access the control from an HTML page:
    <OBJECT id="theId" classid="YourControl.dll#NameSpace.NameOfControl">
    </OBJECT>
back to the top

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

311299 PRB: Cannot Retrieve Top-Level IWebBrowser2 Interface from a .NET UserControl


For more information about how to develop Web-based solutions for Internet Explorer, browse to the following Microsoft Web sites:


back to the top

Modification Type:MajorLast Reviewed:4/21/2006
Keywords:kbdownload kbHOWTOmaster KB311295 kbAudDeveloper