Microsoft KB Archive/311295

= HOW TO: Automate Internet Explorer Within a Contained Visual Basic .NET UserControl =

Article ID: 311295

Article Last Modified on 4/21/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q311295





For a Microsoft Visual Basic 6.0 version of this article, see 181075.

IN THIS TASK

 * SUMMARY
 * How to Run the Sample
 * Technical Notes
 * REFERENCES



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:

Download 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  on a local intranet. To set the security permissions in that scenario, follow these steps:  Start the .NET Framework configuration tool, Mscorcfg.msc. 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. Name the new code group TestVBNetCtrl, and then click Next to display the Choose a condition type dialog box. Select URL as the condition type, and then type http:// /* in the URL box. Click Next to proceed to the Assign a Permission Set to the Code Group dialog box. Click Use existing permission set, click FullTrust, and then click Next to finish.</li></ol>

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:// , and then browse to http:// /testautoie.htm.

back to the top

Technical Notes
<ul> 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

\Microsoft.NET\Primary Interop

where  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.</li> 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 &quot;How to Run the Sample&quot; section of this article.</li>  .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 </li>  The following is the syntax to access the control from an HTML page: <OBJECT id=&quot;theId&quot; classid=&quot;YourControl.dll#NameSpace.NameOfControl&quot;> </OBJECT> </li></ul>

back to the top <div class="references_section">