You can download the source code for this example at
http://www.unboxedsolutions.com/sean/files/VBBehavior.zip
- Create a new ActiveX Dll project and rename the
project to MyVBBehavior
- Rename the default Class1 to CBehavior.
- Add the following references:
Microsoft HTML Object Library - C:\WINDOWS\System32\MSHTML.TLB
Microsoft Internet Controls - C:\WINDOWS\System32\shdocvw.dll
VB IObjectSafety Library - objsafe.tlb (See KB
article
Q182598 from MSDN)
- In the CBehavior class, implement the following
interfaces:
'Interfaces
Implements IElementBehavior
Implements IElementBehaviorFactory
Implements IObjectSafetyTLB.IObjectSafety
- In the CBehavior Class, declare the following
variables:
Private
mobjElementBehaviorSite As
MSHTML.IElementBehaviorSite
Private WithEvents mobjTheElement As MSHTML.HTMLBody
Private mobjElementBehaviorSite As
MSHTML.IElementBehaviorSite
Private WithEvents mobjTheElement As MSHTML.HTMLBody
Private WithEvents mobjDivElement As
MSHTML.HTMLDivElement
Private WithEvents mButtonElement As
MSHTML.HTMLButtonElement
- In the CBehavior Class, type the following code:
Private Sub
IElementBehavior_Init(ByVal pBehaviorSite As
MSHTML.IElementBehaviorSite)
On Error Resume Next
Set mobjElementBehaviorSite = pBehaviorSite
End Sub
Private Sub IElementBehavior_Notify(ByVal lEvent As
Long, pVar As Variant)
On Error Resume Next
If lEvent = 1 Then
Set mobjTheElement =
mobjElementBehaviorSite.GetElement
Call HookPageElements
End If
End Sub
Public Sub HookPage(obj As Object)
Set mobjTheElement = obj
Call HookPageElements
End Sub
Private Sub HookPageElements()
Set mobjDivElement =
mobjTheElement.Document.getElementById("MyDivTag")
Set mButtonElement =
mobjTheElement.Document.getElementById("MyButtonElement")
End Sub
Private Function mButtonElement_onclick() As Boolean
mobjDivElement.innerText = "Hello World!"
End Function
Private Sub IElementBehavior_Detach()
Set mobjTheElement = Nothing
End Sub
Private Function
IElementBehaviorFactory_FindBehavior(ByVal
bstrBehavior As String, ByVal bstrBehaviorUrl As
String, ByVal pSite As MSHTML.IElementBehaviorSite)
As MSHTML.IElementBehavior
Set IElementBehaviorFactory_FindBehavior = Me
End Function
Private Sub
IObjectSafety_GetInterfaceSafetyOptions(ByVal riid
As Long, pdwSupportedOptions As Long,
pdwEnabledOptions As Long)
End Sub
Private Sub
IObjectSafety_SetInterfaceSafetyOptions(ByVal riid
As Long, ByVal dwOptionsSetMask As Long, ByVal
dwEnabledOptions As Long)
End Sub
- Save the MyVBBehavior project.
- From the File menu, click Make MyVBBehavior.dll
to build the DLL.
- From the Project menu, click MyVBBehavior
Properties and click the Component tab. In the
Component tab, make sure Binary Compatibility is
set. This will allow us to interactively debug and
edit-and-continue (something you can't do in VC++).
- Save the project.
- From the Visual Studio 6 Tools folder, start the
OLE View application.
- In OLE View, click the View TypeLib option and
open MyVBBehavior.dll.

- At the very bottom of the IDL file, you should
see a coclass definition. Copy the UUID value
defined above it. In this example, that would be
"6C96BAD2-6A57-4556-94E7-488AB5326B45". You will
need this GUID for the Web page that instantiates
our behavior.
[
uuid(6C96BAD2-6A57-4556-94E7-488AB5326B45),
version(1.0)
]
coclass CBehavior {
[default] interface _CBehavior;
interface IElementBehaviorFactory;
interface IObjectSafety;
interface IElementBehavior;
};
- Create and save the following HTML as
my_vb_behavior.htm, however MAKE SURE YOU SUBSTITUTE
the CLASSID GUID value with the GUID value you
copied from the OLE View application.
<HTML XMLNS:CUSTOM>
<HEAD>
<TITLE>My VB Behavior</title>
<object ID=objBehavior CLASSID="clsid:6C96BAD2-6A57-4556-94E7-488AB5326B45">
</object>
</HEAD>
<BODY id="theBody">
<div id="MyDivTag"></div>
<br>
<input type="button" id="MyButtonElement"
value="Fill DIV">
<script language='vbscript'>
objBehavior.HookPage thebody
</script>
</BODY>
</HTML>
- Save the HTML file and open it in Internet
Explorer.
- Click the Fill DIV button.

- Close Internet Explorer.
- Set a breakpoint in the VB IDE in the HookPage
method.
- Start the debugger.
- Open the My_vb_behavior.html page in Internet
Explorer and you will be ready to leverage the
edit-and-continue ability in VB to develop your
behavior!
