Microsoft Knowledge Base Email Alertz

How to call a VBScript function on a Web page that is loaded in an ActiveX WebBrowser control in a Windows Forms application by using Visual C#

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 922275 - Last Review: November 22, 2007 - Revision: 1.8

How to use Visual C# to call a VBScript function on a Web page that is loaded in an ActiveX WebBrowser control in a Windows Forms application

On This Page

INTRODUCTION

This article describes how to use Microsoft Visual C# to call a Microsoft Visual Basic Scripting Edition (VBScript) function on a Web page. Specifically, the Web page is loaded in an ActiveX WebBrowser control in a Windows Forms application.

To perform the procedures in the "More Information" section, you must already have the following items:
  • A Visual C# Windows Forms application that contains an ActiveX WebBrowser control.

    For more information about how to add an ActiveX control to a Windows Forms application, visit the following Microsoft Developer Network (MSDN) Web site:
    http://msdn2.microsoft.com/en-us/library/1w560tdz.aspx (http://msdn2.microsoft.com/en-us/library/1w560tdz.aspx)
  • A Web page that contains at least one VBScript function.

    For more information about VBScript, visit the following MSDN Web site:
    http://msdn2.microsoft.com/en-us/library/t0aew7h6.aspx (http://msdn2.microsoft.com/en-us/library/t0aew7h6.aspx)
Note The procedures in this article apply only to Windows Forms applications that contain an ActiveX WebBrowser control. If you are using a managed Windows Forms WebBrowser control in your application, do not perform the procedures in this article. For more information about the Windows Forms WebBrowser control, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/2te2y1x6.aspx (http://msdn2.microsoft.com/en-us/library/2te2y1x6.aspx)

MORE INFORMATION

The following procedures demonstrate how to call a VBScript function on a Web page that is loaded in an ActiveX WebBrowser control.

Add a reference to the Microsoft HTML Object Library (Mshtml.tlb)

Before you can call a VBScript function in an ActiveX WebBrowser control from a Windows Forms application, you must add a reference to the Microsoft HTML Object Library. To do this, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to Open, and then click Project.

    Note In Visual Studio 2005, click Project/Solution.
  3. Locate the project that you want to open, and then click Open.
  4. On the Project menu, click Add Reference.
  5. Click the COM tab.
  6. Select Microsoft HTML Object Library, click Select, and then click OK.

    Note In Visual Studio 2005, select Microsoft HTML Object Library, and then click OK.

Add a method that calls the VBScript function

After you add a reference to the Microsoft HTML Object Library, you can add a method that calls a VBScript function on a Web page that is loaded in an ActiveX WebBrowser control.

The following code sample demonstrates how to call a VBScript function on a Web page that is loaded in an ActiveX WebBrowser control. This code sample uses the IHTMLDocument.Script property in the Microsoft HTML Object Library to obtain access to the scripting object. The code sample then uses the InvokeMember method to call the VBScript function.

Note Replace axWebBrowser1 in the following code with the name of the ActiveX WebBrowser control instance that occurs in your application.
private object InvokeScript(string sFunction,object[] oParameters)
{
    object oReturn = null;
    mshtml.IHTMLDocument Doc = (mshtml.IHTMLDocument)this.axWebBrowser1.Document;
    Type ScriptEng = Doc.Script.GetType();
    try
    {
        oReturn = ScriptEng.InvokeMember(sFunction, System.Reflection.BindingFlags.InvokeMethod, null, Doc.Script, oParameters);
    }
    catch(Exception ex)
    {
        MessageBox.Show("Error invoking function: " + ex.Message,"InvokeScript - Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    return oReturn;
}
You can call this method by passing the name of the VBScript function and the parameters. The following code sample demonstrates how to use the previous code sample to call a VBScript function that has a parameter value of 100.

Note The VBScript function is named myVBScriptFunction.
String myFunction = "myVBScriptFunction";
Object[] myParameters = { 100 };
InvokeScript(myFunction, myParameters);

REFERENCES

For more information about the ActiveX WebBrowser control, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/Aa752040.aspx (http://msdn2.microsoft.com/en-us/library/Aa752040.aspx)

APPLIES TO
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbwindowsforms kbhowto kbinfo KB922275
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

arvind - arvindharitus NOSPAM-AT-NOSPAM yahoo.com Report As Irrelevant  
Written: 8/24/2006 2:51 AM
please give solution in detail. i can't found how can i input the script or where we define function. please give this article me in detail

Anonymous User Report As Irrelevant  
Written: 2/19/2010 2:14 AM
Text Only Please