Working with IHTMLDocument2...

I am working on a project that requires us to have a web browser embedded in our windows application.  This is relatively easy to do if you are using the load to bring in an external URL or a URL pointing to temp file.  But I wanted to be able to use an XSLT stylesheet and transform some xml into HTML, without saving to a temp file.  I found you are able to use the GetBody and change the innerHTML of that element, but you can't change anything above body.   (I want to change the inner/outer HTML of the Document.)

I started parsing the html to be displayed using regex and pulling out the script and style elements and putting them into the body.  (IE usually allows for this kind of thing)  But I am not getting it to work exactly the way I want.

I also tried writing the style using CreateStyleSheet() but it thinks the css I am giving it is a URL instead of code.

If anyone knows of a way to implement write() on the document or an easier way of doing this, please let me know!!

p.s.  We are not using the ActiveX control.  We are wrapping the COM object in order to get around a bug with the ActiveX control displaying in docking controls. 

Published Tuesday, August 24, 2004 9:39 AM by dotnetboy2003

Comments

# re: Working with IHTMLDocument2...

at one point I did a test with a .net warapper that seemed to work well...

I don't have the original link handy but it was a .net control used by the SharpDevelop folks.

can you host a private copy of the asp.net server in the app?

I have heard that you can use that to generate pages ... the trick is to hook the web browser to the asp.net with streams or pipes to avoid writing to disk.

Tuesday, August 24, 2004 10:13 AM by denny

# re: Working with IHTMLDocument2...

Yea, I tried the SharpDevelop wrapper. They put an HTML property on the browser but it actually just puts the value into the body not the document. I will investigate the stream avenue and see if I can come up with anything. Thanks!!

Tuesday, August 24, 2004 12:15 PM by Brenton House

# re: Working with IHTMLDocument2...

the stuff I saw was about 1 to 1.5 years back.

it was a dll made by someone else that used the MS IE com bits and if you want to it you could do all kinds of stuff....

they may have changed that part as I know they used to use a gui lib. that they dumped.

I'll see If i can find the dll / net thing...

Tuesday, August 24, 2004 3:55 PM by denny

# re: Working with IHTMLDocument2...

herer is the link:
http://www.itwriting.com/htmleditor/index.php

see if it helps

Tuesday, August 24, 2004 3:58 PM by denny

# re: Working with IHTMLDocument2...

What language are you doing it in, and what exactly do you want to do?

You can definately change stuff above the body tag, it's just that the body tag is immediately available to you (document.body).

If you want to modify stuff in the head, then you can do:
document.getElementsByTagName("HEAD").item(0)
and you'll have an IHTMLElement pointer to the head element. getElementsByTagName returns an array, so you should be checking that a) if it returned any and b) if it returned more than one (yes, i've seen multiple head elements come back).
You also do a document.getElementsByTagName("HTML") to start iterating from the top :)

You to strip out styles and scripts by using a combination of the IHTMLElement and IHTMLDomNode interfaces (all elements have the IHTMLElement and IHTMLDomNode interfaces) can calling removeChild on an elements parent domnode.

As for gettign xml transformed into html into the document, i know i've done it before, somwhere :) if you're still stuck contact me direct and i can possibly give you some pointers....i warn you, i'm a vb6/vb.net guy however :)

Tuesday, August 24, 2004 7:50 PM by Geoff Appleby

# re: Working with IHTMLDocument2...

Man... Seriously... we were doing this 3 years ago in Orion... ;) Mark shoulda told you that... :P

I'm pasting the class here, if it gets jacked up, let me know and I'll email it to ya..

Option Strict Off

Public Class BrowserWrapper
Private m_browser As AxSHDocVw.AxWebBrowser
Private m_currentDocument As Object
Private Const BASEHTML As String = "<html><head><title></title></head><body></body></html>"

Public Function GetUnderlyingOCX() As Object
Return m_browser.GetOcx()
End Function

Public Sub New(ByVal browser As AxSHDocVw.AxWebBrowser)
m_browser = browser
m_browser.Navigate("About:blank")
m_currentDocument = m_browser.Document
'm_browser.Document.createStyleSheet("D:\Orion\Source Code\GUI\MediaBrowser\Transforms\html.css", 0)
End Sub

Public Sub Write(ByVal htmlString As String)
Clear()
m_currentDocument.write(htmlString)
End Sub

Public Sub Append(ByVal htmlString As String)
m_currentDocument.Write(htmlString)
End Sub

Public Sub Clear()
Try
m_currentDocument.write("")
m_currentDocument.close()
Catch ex As NullReferenceException
m_browser.Navigate("About:blank")
Catch ex As Exception
Throw ex
End Try
End Sub

Private Sub OpenDocument()
m_currentDocument = m_browser.Document
End Sub

Public Sub Refresh()
m_browser.ExecWB(SHDocVw.OLECMDID.OLECMDID_REFRESH, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT)
End Sub

Public ReadOnly Property Document() As Object
Get
Return m_currentDocument
End Get
End Property
End Class

Tuesday, August 31, 2004 10:36 PM by Don

# re: Working with IHTMLDocument2...

This only works if you are using the activex control.

Also, createStyleSheet works fine if you give it a file, but I was trying to give it the actual style text.

Thanks though!

Wednesday, September 01, 2004 9:20 AM by Brenton House

Leave a Comment

(required) 
(required) 
(optional)
(required)