Sunday, April 15, 2012

(c# create html document) C# -- dynamically create and open a HTML page using the InternetExplorer.Application

(1) Add Reference-> Browse -> \windows\system32\SHDocVw.dll
(2) Add Reference-> .NET -> Microsoft.mshtml

(3) import...
import SHDocVw;
import mshtml;

(4) sample code...

System.Type t = System.Type.GetTypeFromProgID("InternetExplorer.Application");
SHDocVw.IWebBrowser2 ie = System.Activator.CreateInstance(t) as SHDocVw.IWebBrowser2;
...
// setup 
ie.Visible = true;
ie.MenuBar = false;
ie.AddressBar = false;
ie.ToolBar = 0;
ie.Width = 800;
ie.Height = 600;
...
// navigate
object _ =0;
object url = "about:blank";
ie.Navigate2(ref url, ref _, ref _,  ref _, ref _);
...
// create HTML page
mshtml.IHTMLDocument2 doc = (mIE.Document as mshtml.IHTMLDocument2);
doc.write("<html><body>It works!</body></html>");