user863952
user863952

Reputation: 113

Displaying HTML Content with a Web Browser Control in Visual Basic

I need to display the data in VB screen. I am using WebBrowser control.

Then I am using this command:

m_oFrm.WebBrowser1.Document.Write (SHtml) 

to display the output on screen.

Statement is going fine in debug mode but in EXE/DLL it's giving error

Object or Variable not Set

I used an alternative option as:

Dim objHTMLDocScore As MSHTML.HTMLDocument
Set objHTMLDocScore  = m_oFrm.WebBrowser1.Document

And then

Document.Write (SHtml)

But I am getting the error while compiling as

Function or interface marked as restricted, or the function uses an automation type not supported in Visual Basic

I can see there is a question in StackOverflow for error message "Function or interface marker as restricted, or the function uses an automation type not supported in Visual Basic" but I am not able to understand the same.

I am using the Internet Explorer version 8.

Upvotes: 1

Views: 13784

Answers (3)

user7212232
user7212232

Reputation: 115

Assuming your webbrower is [WB]

private sub subOpenHtml(byval sHtml as string)

    WB.Navigate "about:blank" 'creates a document to do something on
    subBrowserStopClickSound 'stops ie click sound '(not necessary but a nice touch so users of your program don't know you are hijacking IE.

setdocretry:

    If WB.ReadyState = READYSTATE_COMPLETE Then
         If Not (WB.Document Is Nothing) Then
               WB.Document.open
               WB.Document.write sHtml
               WB.Document.Close
         Else
               Stop'error handling
         End If
    Else
        Pause.subFor 1
        GoTo setdocretry:
    End If

end sub 

Code for subBrowserStopClickSound (separate module good idea):

At top of Module:

Private Declare Function CoInternetSetFeatureEnabled Lib "urlmon.dll" (ByVal FeatureEntry As Integer, ByVal dwFlags As Long, ByVal fEnable As Long) As Long
Private Const FEATURE_DISABLE_NAVIGATION_SOUNDS = 21
Private Const SET_FEATURE_ON_PROCESS As Integer = &H2

Below that:

private sub subBrowserStopClickSound 
    Call CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, True)
end sub 

The code for Pause:

Again, in a separate module (I find this code to be very useful in a lot of situations and is a better and more controllable alternative to DoEvents)

Top of Module:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Type variables
               loldSecond As Long
End Type
Dim v As variables

Below that:

Sub subFor(ByVal Delay As Single)

    Dim seconds As Long
    v.loldSecond = 0

    Delay = (Timer + Delay)

    Do
        DoEvents: DoEvents: DoEvents: DoEvents

         'tells us how many seconds left without decimal
         seconds = (Delay - Timer)

         Sleep 10
    Loop While Delay > Timer

End Sub

Sorry if I neglected something in the code, if so, please let me know so I can correct.

Upvotes: 0

Bob77
Bob77

Reputation: 13267

Often the DHTML Edit control works better for such things:

Private Sub Form_Load()
    With DHTMLEdit1
        .BrowseMode = True
        .DocumentHTML = "<html><body>Hello World!</body></html>"
    End With
End Sub

Of course you'll want the newer (post XP) TriEdit package from Microsoft and most likely the associated SDK documentation.

Upvotes: 0

GSerg
GSerg

Reputation: 78155

You are trying to write to a document that hasn't yet constructed. When you debug, the browser has time to construct the object, otherwise it does not.

Check the readyState property before writing.

If you want to write before navigating anywhere, then first navigate to about:blank, wait for readyState, then write.

Upvotes: 3

Related Questions