Where is the IHTMLScriptElement?

I just started using Visual Studio 2010 C # IDE today.

I am trying to insert javascript into a WebBrowser component and follow the response from stackoverflow:

How to add Javascript to a WebBrowser control?

HtmlElement txtBox = webBrowser1.Document.GetElementById("UserEmailShortcut"); //js HtmlElement scriptOne = webBrowser1.Document.CreateElement("script"); IHTMLScriptElement element = (IHTMLScriptElement)scriptOne.DomElement; element.text = "function sayHello() { alert('hello') }"; txtBox.AppendChild(scriptOne); webBrowser1.Document.InvokeScript("sayHello"); 

Now there is a red wavy line under the IHTMLScriptElement saying:

  Error 1 The type or namespace name 'IHTMLScriptElement' could not be found (are you missing a using directive or an assembly reference?) C:\Users\m-tak\documents\visual studio 2010\Projects\winformWithWebBrowserTest\winformWithWebBrowserTest\Form1.cs 25 13 winformWithWebBrowserTest 

I looked at the documentation http://msdn.microsoft.com/en-us/library/aa768904(v=VS.85).aspx

but I can’t understand why. Isn't that already included and just need to be included as " using IHTMLScriptElement " (which didn't work ..)

+20
c #
Mar 07 2018-11-11T00:
source share
4 answers

Project + Add link, select Microsoft.mshtml. If it does not appear in the list (this is the PIA), use the Browse tab and select c: \ windows \ system32 \ mshtml.tlb

Add a usage directive at the top of the source code file:

 using mshtml; 

Remember that you have deployment details. If the PIA is not installed on the target machine, which is unlikely, it is best to set the Copy Local property to reference the assembly to True. This creates the Microsoft.mshtml.dll file in the assembly folder. Copy it along with the EXE to the target machine. There are several cases where a PIA is required, not this one.




An update for this old post, .NET 4+ and VS2010 + now support the insert assembly interaction property of the link assembly. Also known as the No PIA feature. This obviates the need for PIAs and is the best way to work with interop assemblies. It is set to True automatically if your project is not running in the old version of VS.

It has no downstream sides, you no longer need to deploy the interop assembly, and the types of interaction you use are now embedded in your executable. Only the ones you use. Your code may need a little change, if you are now creating a COM object using new XxxxClass() , then you need to remove part of the class name.

+31
Mar 07 2018-11-11T00:
source share
β€” -

IHTMLScriptElement is defined in Mshtml. You need to add a link to the Microsoft HTML object library, and then add "using mshtml" for VS to enable IHTMLScriptElement.

+2
Mar 07 2018-11-11T00:
source share

I would recommend this answer for a javascript-only input-driven solution that does not require PIA deployment, like mshtml.

+2
May 14 '14 at 6:43
source share

It is located in the Mshtml.dll file.

The System.Windows.Forms.HtmlElement DomElement property has a type object, but is actually a COM IUnknown pointer for this element, which can be applied to one of the interfaces of the HTML element, for example mshtml.IHTMLScriptElement .

+1
Mar 07 2018-11-11T00:
source share



All Articles