Fog Creek Software
Discussion Board

@%*!?$# MSHTML

I have a need to do simple WYSIWYG HTML editing in a VB6 Project.  So far I've tried putting a MS WebBrowser control in edit mode, and using the DHTML Edit Control (IE5).

The problems happen when I try to access the attributes of the currently selected element.  When I detect that a control has been selected (Document_onselectionchange) I loop thru known attributes of a particular tag and use IHTMLElement.getAttribute() to get a value.

This works fine for about 1 minute and then it just crashes the IDE saying that a portion of the memory couldn't be "Read".

I've been reading all day about how much MSHTML.DLL crashes when used with VB, does anyone know of a safe subset of objects/methods that can be used to manipulate the DOM?

Otherwise, does anyone know of a nice WSYIWYG HTML Com Object that I can use?


Monday, January 13, 2003

Here's some sample code that worked for a while but then started crashing the IDE again after a few minutes of use:

'  This is called after detecting a CONTROL select:
Private Sub HTMLControlSelect()
    Dim oRange As MSHTMLCtl.IHTMLControlRange
    If webEdit.Busy Then
        Debug.Print "Document Not Ready.  Exiting."
        Exit Sub
    End If
    Set oRange = moHTMLDoc.selection.createRange()
    frmProps.BrowseHTML moBranch, oRange.Item(0)
End Sub

'  This is frmProps.BrowseHTML:
Public Sub BrowseHTML(oBranch As DCBSC.CBranch, oElement As MSHTMLCtl.IHTMLDOMNode)
    Dim oAttrib As DCBSEdit.CAttribute, oTagDef As DCBSEdit.CTagDefinition
    Dim oAtts As MSHTMLCtl.IHTMLAttributeCollection, oAtt As MSHTMLCtl.IHTMLDOMAttribute
    Dim i&, j&
    Dim sTag$, sNode$, vNode As Variant
    On Error Resume Next
    meOTCur = dcbse_ot_HTML
    sTag = oElement.nodeName
    Set moBranch = oBranch
    Set moScript = oBranch.Parent
    Set oTagDef = goApp.TagDefinitions(sTag)
'  Add a group for the Branch and select it.
    With Browser.PropertyGroups
        .Add "HTML: " & sTag
        .Index = 0
    End With
    Set oAtts = oElement.Attributes
    j = oAtts.length - 1
    For i = 0 To j
        Set oAtt = oAtts.Item(i)
        With oAtt
            sNode = .nodeName
            vNode = .nodeValue
        End With
        If oTagDef.Exists(sNode) Then
            Set oAttrib = oTagDef(sNode)
            With oAttrib
                Select Case .valueType
                Case dcbse_at_ENUMERATED

                    HTMLListOptions oTagDef, Browser.AddProperty(.Title, .Title, pvpList, vNode)
                Case dcbse_at_CSSSTYLE, dcbse_at_STYLE, dcbse_at_RELATIVEPATH, _
                    dcbse_at_FLAG, dcbse_at_FONT, dcbse_at_TEXT, dcbse_at_VARIANT
                    Browser.AddProperty .Title, .Title, pvpString, vNode
                Case dcbse_at_COLOR
                    Browser.AddProperty .Title, .Title, pvpColor, vNode
                Case dcbse_at_EVENT
                    Browser.AddProperty .Title, .Title, pvpString
                End Select
            End With
        End If

End Sub

Monday, January 13, 2003

Which line of code does it specifically blow up on?
What happens if you run it outside the IDE?
Why are you using On Error Resume Next instead of structured error handling?

The cause could be related to any number of these....

Better than being unemployed...
Tuesday, January 14, 2003

Sorry, I posted this at like 3AM.  Posting to a messageboard is usually my last used resource and my netiquette is a little rusty.  Furthermore, I think that my problem was a combination of things, not just MSHTML.

>>Which line of code does it specifically blow up on?
I'm still not sure because it is very sporadic and just crashes the IDE.  It's not in the same place everytime.

>>What happens if you run it outside the IDE?
The same thing, "Memory couldn't be read at..." message.

>>Why are you using On Error Resume Next instead of >>structured error handling?
Because it was ignoring my error handling anyway, and with On Error Resume Next, sometimes the Windows Error Logging service would catch it so I could at least see the error report.  This was temporary.

Okay, I've gotten around this problem.  Instead of querying the element for each attribute value, I just take the selected element's outerHTML property and parse it.  I wrote a few functions to parse HTML tag names and attributes.  This is easy because the HTML that you get from outerHTML seems to be pretty tidy (no pun intended). I think this is better anyway because you never know when Microsoft is going to change some implied behavior.

Also, it seems that I was having some problems with Infragistics Property Browser control (I'm still not sure what but I don't care), so I dropped it and I made my own with a combo box and a VSFlexgrid.

So, sorry for the hasty, angry, malformed post and thanks for your replies!

Tuesday, January 14, 2003


Only four messages in the thread until this one, yet the home forum page states there are seven messages.

Yet four messages have now been deleted.

Any explanation?

Stephen Jones
Wednesday, January 15, 2003

And now my two messages are back but the original "deleted?" two messages from Wei and Wayne still aren't showing up.

I resolve henceforth never to post to any thread with "@%*!?$#" in the title; tempting fate too far!

Stephen Jones
Wednesday, January 15, 2003

Yes, I knew I was tempting fate when I posted ""@%*!?$#", so I copied everything I wrote to the clipboard before posting in case the server had an error.

The reason I went against my better judgement was because I wanted to see if it would work, because I wanted to see if, for all of his talk about user-centric design he made the process human enough to accept the exact characters that I wanted to post.

Joel & co. passed with flying colors...not even a hiccup when I sent those questionable characters.

The reason that two other posts are missing is because somebody moderated them out.

Wednesday, January 15, 2003

Ok quick question...

I am doing something similar manipulating an html page for my UI. Now I am having one simple problem..

When the webbrowser control fires it's navigate complete event on a local page, it does not for some reason put a valid pointer to its current document property. Now I am using 2 webbrowser controls in the same form which may have something to do with it. Also if it (control) sees itself as being offline,(No Network Connection) it will not fire a  navigate complete event at all.

I am starting to think this control is quite the slice of %^*&.

I am using the navigate complete event with the MSHTML DOM library to manipulate buttons and span elements with in the current document but it is very sporadic and also just sudddenly crashes the ide without warning.

If anyone has a resolution it would be nice.

Or a better control.

George Tarrant
Wednesday, July 30, 2003

*  Recent Topics

*  Fog Creek Home