Displaying a HTMLDocument in a WebBrowser control

Hi,

How can one display a HTMLDocument created outside of the WebBrowser control in the control? I try the .Document property, but its read-only?

I have like:

dim a as HTMLDocument
dim b as HTMLDocument
dim c as HTMLDocument

if xyz then
  WebBrowser1.Document = a
else if zyx then
  WebBrowser1.Document = b
...
 
That kind of thing. What I also do tho is load the HTML document into a HTMLDocument object, manipulate it, then either send it straight to the print, or dhow it in a WebBrowser ( for "print preview" ).

Any ideas?

Many thankx in advance,
Mark Hewitt
LVL 3
mh2Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Richie_SimonettiConnect With a Mentor IT OperationsCommented:
This works:

Option Explicit

Dim a As New HTMLDocument
Private Sub Form_Load()
Dim ff As Integer
ff = FreeFile
Open "c:\temp.htm" For Input As ff
a.body.innerHTML = Input(LOF(ff), 1)
Close ff
With WB1
    .Navigate "about:blank"
    Do While .ReadyState <> READYSTATE_COMPLETE
        DoEvents
    Loop
    .Document.body.innerHTML = a.documentElement.innerHTML
End With
End Sub

Private Sub WB1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is WB1.Object) Then
   
End If
End Sub
0
 
WolfgangKoenigCommented:
I think you must save the edited document and then reload
the document in your WebBrowser object...

WoK;)
0
 
Richie_SimonettiIT OperationsCommented:
Use .navigate event for each one.
Where are a, b and c stored?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mh2Author Commented:
Hi,

The actual documents are in memory, I don't want to/can't load the WebBrowser control from a file.

Heres an example:

'-------------------------------------------

' load the report template
Dim A as HTMLDocument
set A = tmp.CreateDocumentFromURL("file://c:\reports\tempalte.html")

' fill in our fields based on user input and stuff
A.all("airbase").innerText = "Cliffview A102"

' choose one of two paths, print direct or print preview
if ( bUserChoseToPreview ) then

  ' display it in a web browser to simulate "print preview"
  ' the preview will IRL popup a dialog, the form with
  ' the print button in it wont have a WebBrowser control
  ' hence the need for a generic HTMLDocument above
  set WebBrowser1.Document = A   ' fails: .document is read-only???
else
  A.execCommand "Print"     ' go straight to printer
 
end if

'-------------------------------------------

So basically, I am looking to create a print-preview mechaism after I have updated a HTMLDocument object. If there is a better way I'd be more than happy to know!

Thankx,
Mark
0
 
Richie_SimonettiIT OperationsCommented:
I am not sure but try:
WebBrowser1.Document.body.innerhtml = A.documentelement.innerhtml
Before access WebBrowser1.Document it must be a really document in webbrowser control or the method will fails.
You could do the following

WebBrowser1.navigate "about:blank"
do while WebBrowser1.ReadyState <> READYSTATE_COMPLETE
   doevents
loop

WebBrowser1.Document.body.innerhtml = A.documentelement.innerhtml
0
 
Richie_SimonettiIT OperationsCommented:
hi, any problem with that?
0
 
mh2Author Commented:
Hi,

Sorry it took so long, my machine crashed and I haven't been able to work for the past 2 days or so!

It woprked perfectly, I'm feeling really stupid, such an obvious thing to do, now why didn't I think of that?? :)

Thankx again for your help,
Mark
0
 
Richie_SimonettiIT OperationsCommented:
You are welcome.
Thanks for "A" grade!
0
 
BuchankasCommented:
Hello guys...

I got the same question as mh2, however I haven't fully figured out Simonetti's code. I am trying to load a crude HTML code from a normal textbox to the WebBrowser Control, is tha possible?

Thanks =o)
0
 
Richie_SimonettiIT OperationsCommented:
Buchankas: did you set a reference to Microsoft HTML object lbrary in your project?
0
 
BuchankasCommented:
Yes, I did... the problem I am facing now is that my WebBrowser1 control (WB1 in your code), doens't show the "Document.body.innerHTML" when I write "WebBrowser1" and press the dot key... it should list all properties, methods available, but it doesn't... so something is wrong...

How did you declare and set this WB1? I presume it stands for the WebBrowser1 Control, right?
0
 
Richie_SimonettiIT OperationsCommented:
Nothing is wrong and yes, you are right. WB control doesn't displays anything beyond Document property.

try this

Private Sub WB1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is WB1.Object) Then
   ' change de name to your HTMLDocument variable
   set wbdoc = wb1.document
   ' and now, use the dot with wbdoc variable...
End If
End Sub
0
All Courses

From novice to tech pro — start learning today.