The following code is the VB.NET version of the C code in the KB article
249232
- HOWTO: Get IHTMLDocument2 from a HWND. It requires oleacc.dll
to be installed on the system (oleacc.dll is part of the Active Accessibility).

Edanmo.IEDom
Module
Imports mshtml
Imports System.Text
Imports System.Runtime.InteropServices
Namespace Edanmo
Public Module IEDom
Declare Ansi Function GetClassName Lib "user32" _
Alias "GetClassNameA" ( _
ByVal hWnd As IntPtr, _
ByVal lpClassName As StringBuilder, _
ByVal nMaxCount As Int32) As Int32
Delegate Function EnumChildProc( _
ByVal hWnd As IntPtr, _
ByRef lParam As IntPtr) As Int32
Declare Function EnumChildWindows Lib "user32" ( _
ByVal hWndParent As IntPtr, _
ByVal lpEnumFunc As EnumChildProc, _
ByRef lParam As IntPtr) As Int32
Declare Ansi Function RegisterWindowMessage Lib "user32" _
Alias "RegisterWindowMessageA" ( _
ByVal lpString As String) As Int32
Declare Ansi Function SendMessageTimeout Lib "user32" _
Alias "SendMessageTimeoutA" ( _
ByVal hWnd As IntPtr, _
ByVal msg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32, _
ByVal fuFlags As Int32, _
ByVal uTimeout As Int32, _
ByRef lpdwResult As Int32) As Int32
Const SMTO_ABORTIFHUNG As Int32 = &H2
Declare Function ObjectFromLresult Lib "oleacc" ( _
ByVal lResult As Int32, _
ByRef riid As System.Guid, _
ByVal wParam As Int32, _
ByRef ppvObject As IHTMLDocument) As Int32
Public Function IEDOMFromhWnd(ByVal hWnd As IntPtr) As IHTMLDocument
Dim IID_IHTMLDocument As System.Guid = New System.Guid("626FC520-A41E-11CF-A731-00A0C9082637")
Dim hWndChild As Int32
Dim lRes As Int32
Dim lMsg As Int32
Dim hr As Int32
If Not hWnd.Equals(0) Then
If Not IsIEServerWindow(hWnd) Then
' Get 1st child IE server window
EnumChildWindows(hWnd, AddressOf EnumChild, hWnd)
End If
If Not hWnd.Equals(0) Then
' Register the message
lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")
' Get the object
Call SendMessageTimeout(hWnd, lMsg, 0, 0, _
SMTO_ABORTIFHUNG, 1000, lRes)
If lRes Then
' Get the object from lRes
hr = ObjectFromLresult(lRes, IID_IHTMLDocument, 0, IEDOMFromhWnd)
If hr Then Throw New comexception(hr)
End If
End If
End If
End Function
Private Function EnumChild(ByVal hWnd As IntPtr, ByRef lParam As IntPtr) As Int32
If IsIEServerWindow(hWnd) Then
lParam = hWnd
Else
EnumChild = 1
End If
End Function
Private Function IsIEServerWindow(ByVal hWnd As IntPtr) As Boolean
Dim Res As Int32
Dim ClassName As StringBuilder = New StringBuilder(100)
' Get the window class name
Res = GetClassName(hWnd, ClassName, ClassName.MaxCapacity)
IsIEServerWindow = StrComp( _
ClassName.ToString(), _
"Internet Explorer_Server", _
CompareMethod.Text) = 0
End Function
End Module
End Namespace