How do I cast a System.Windows.Control WebBrowser.Document to an mshtml.MSHTMLDocumentClass?
Asked Answered
S

1

6

I have a WebBrowser that loads inside a WPF window. I need to get the title of the web page loaded in the WebBrowser.

I get the document using

object doc = this._browser.Document; and I can see that it is an mshtml.MSHTMLDocument and I want to cast it as this type so that I can pull the title out, however I can't find this type in any .NET library.

Will I have to create the type myself or am I just looking in the wrong place/approaching this wrong way?

How can I pull the page title out of a System.Windows.Controls.WebBrowser Document?

Swetiana answered 30/1, 2014 at 19:56 Comment(0)
G
15

Either add reference to Microsoft.mshtml and then:

var title = (webBrowser.Document as mshtml.HTMLDocument).title;

or

dynamic doc = webBrowser.Document;
var title = doc.title;
Gerius answered 30/1, 2014 at 20:15 Comment(6)
which specific .dll contains Microsoft.mshtml?Swetiana
If I go to Add Reference it will be under Extensions -> Miscosoft.mshtmlGerius
We must be using different versions of Visual Studio or different IDEs. I am using Visual Studio 2010 and I don't have Extensions as a tab under Add Reference. Out of curiosity, in what directory is it located on your computer? I am banging my head against my desk trying to find this one file on my system. Maybe I will get lucky and have the same path.Swetiana
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Common\Microsoft.mshtml.dll, also search for Microsoft HTML Object Library as I suggested in one of my old answers hereGerius
Found it. The file was mshtml.tlbSwetiana
VS22, it's found in {Install Directory...}\Common7\IDE\PublicAssemblies. To add DLLs, follow directions here: stackoverflow.com/a/65017892.Judsonjudus

© 2022 - 2024 — McMap. All rights reserved.