Discussion:
HOWTO Use WebBrowser Control to Open & Edit HTML File
(too old to reply)
Dan Johnson
2006-01-21 19:05:57 UTC
Permalink
We're using the Webbrowser control to display html help documents (stored
locally) to our users. We need to be able to access the body text of the
html document both for purposes of editing the text and searching the text.
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not necessarily
have to open/search the html file using the Webbrowser control BTW.
TedF
2006-01-21 21:30:01 UTC
Permalink
To view a webpage source, you need to use the
Inet control (Internet Transfer control), and save it
to a .txt file locally.
But if the webpage is local, then simply copy the html file to a .txt.
Example:
FileCopy "C:\MyPages\Testpage.htm", "C:\MyPages\Testpage.txt"
and then execute it.
Post by Dan Johnson
We're using the Webbrowser control to display html help documents (stored
locally) to our users. We need to be able to access the body text of the
html document both for purposes of editing the text and searching the text.
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not necessarily
have to open/search the html file using the Webbrowser control BTW.
Larry Serflaten
2006-01-21 22:21:24 UTC
Permalink
Post by Dan Johnson
We're using the Webbrowser control to display html help documents (stored
locally) to our users. We need to be able to access the body text of the
html document both for purposes of editing the text and searching the text.
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not necessarily
have to open/search the html file using the Webbrowser control BTW.
This may help you get a little farther along;

On a new form add a webbrowser, a textbox and button for the address
bar, and 2 option buttons to select the output (all using default names).
Then paste in the code below:

HTH
LFS

Private Sub Form_Load()
Command1.Caption = "Go"
Option1.Caption = "HTML"
Option2.Caption = "Text"
Option2.Value = True
Text1.Text = "www.google.com"
End Sub

Private Sub Command1_Click()
WebBrowser1.Navigate2 Text1.Text
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If pDisp = WebBrowser1 Then
If Option1 Then
Debug.Print WebBrowser1.Document.documentElement.outerHTML
Else
Debug.Print WebBrowser1.Document.documentElement.innerText
End If
End If
End Sub
TedF
2006-01-22 08:01:23 UTC
Permalink
That looks good, but you have to reload the page,
don't you ?
Post by Larry Serflaten
Post by Dan Johnson
We're using the Webbrowser control to display html help documents (stored
locally) to our users. We need to be able to access the body text of the
html document both for purposes of editing the text and searching the text.
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not necessarily
have to open/search the html file using the Webbrowser control BTW.
This may help you get a little farther along;
On a new form add a webbrowser, a textbox and button for the address
bar, and 2 option buttons to select the output (all using default names).
HTH
LFS
Private Sub Form_Load()
Command1.Caption = "Go"
Option1.Caption = "HTML"
Option2.Caption = "Text"
Option2.Value = True
Text1.Text = "www.google.com"
End Sub
Private Sub Command1_Click()
WebBrowser1.Navigate2 Text1.Text
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If pDisp = WebBrowser1 Then
If Option1 Then
Debug.Print WebBrowser1.Document.documentElement.outerHTML
Else
Debug.Print WebBrowser1.Document.documentElement.innerText
End If
End If
End Sub
Larry Serflaten
2006-01-22 08:41:01 UTC
Permalink
Post by TedF
That looks good, but you have to reload the page,
don't you ?
Reload the page to do what? As it is it is a demonstration
on using the Document property to find either the text or HTML
of the next loaded page. That should be sufficient to answer his
Post by TedF
Post by Dan Johnson
We need to be able to access the body text of the
html document both for purposes of editing the text and searching the
text. I started palying with the Webbrowser.Document property, but
I'm getting nowhere fast. Can anybody give me reference here?
Dan Johnson
2006-01-22 16:19:46 UTC
Permalink
Thank you Ted and Larry.

I actually need to use elements of both of your ideas. Since I want the
WebBrowser control to be the user interface, I need to open a text file,
make necessary edits to the text, save the revised text, then reopen the
saved file via WebBrowser1.Navigate2. This allows me to customize the
terminology used in the document.

I also need to study the WebBrowser1.Document.documentElement.innerText
property. I hadn't seen this before, and think I might be able to use it to
save a step from above.

For example, assume the text reads as follows in the doc template:

"Take a ride in the [Color] car."

I want to run Replace(MyText, "[Color]", "Blue") and transfer the revised
text to the WebBrowser control, but NOT have the changes saved to the
original document since the [Color] variable might change over time. Is the
WebBrowser1.Document.documentElement.outerHTML property writable so I can
open the *.html document using WebBrowser.Navigate2 then merely rewrite the
outerHTML property? I will need to play with this.

Again, Thanks!
Post by Larry Serflaten
Post by Dan Johnson
We're using the Webbrowser control to display html help documents (stored
locally) to our users. We need to be able to access the body text of the
html document both for purposes of editing the text and searching the text.
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not necessarily
have to open/search the html file using the Webbrowser control BTW.
This may help you get a little farther along;
On a new form add a webbrowser, a textbox and button for the address
bar, and 2 option buttons to select the output (all using default names).
HTH
LFS
Private Sub Form_Load()
Command1.Caption = "Go"
Option1.Caption = "HTML"
Option2.Caption = "Text"
Option2.Value = True
Text1.Text = "www.google.com"
End Sub
Private Sub Command1_Click()
WebBrowser1.Navigate2 Text1.Text
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If pDisp = WebBrowser1 Then
If Option1 Then
Debug.Print WebBrowser1.Document.documentElement.outerHTML
Else
Debug.Print WebBrowser1.Document.documentElement.innerText
End If
End If
End Sub
mayayana
2006-01-22 17:15:28 UTC
Permalink
It's a pain in the neck, due to the complexity of
the IE DOM and the imprecision of HTML.
(If it really worked without problems then WYSIWYG
webpage editors would be easy to write and
would be actually useful.)
On the other hand, there is an amazing amount that you
can do with the IE DOM in a WB. If you set a reference
to MSHTML you'll get intellisense for the whole thing.

This example might be somewhat useful. It's a
WYSIWYG HTML editor written in VBScript - running
in an IE window - for the purpose of demonstrating
DOM functions and properties.

http://www.jsware.net/jsware/scripts.php3#domed

Doing it in VB is almost identical. It just
uses defined DOM objects through the MSHTML
type library, while script doesn't define variables
by type. The script sample might give you what you
want in terms of working out the methods you
need to accomplish what you want to do dynamically
in the page.




--
***@mindXXspring.com
(Remove Xs for return email.)
Post by Dan Johnson
Thank you Ted and Larry.
I actually need to use elements of both of your ideas. Since I want the
WebBrowser control to be the user interface, I need to open a text file,
make necessary edits to the text, save the revised text, then reopen the
saved file via WebBrowser1.Navigate2. This allows me to customize the
terminology used in the document.
I also need to study the WebBrowser1.Document.documentElement.innerText
property. I hadn't seen this before, and think I might be able to use it to
save a step from above.
"Take a ride in the [Color] car."
I want to run Replace(MyText, "[Color]", "Blue") and transfer the revised
text to the WebBrowser control, but NOT have the changes saved to the
original document since the [Color] variable might change over time. Is the
WebBrowser1.Document.documentElement.outerHTML property writable so I can
open the *.html document using WebBrowser.Navigate2 then merely rewrite the
outerHTML property? I will need to play with this.
Again, Thanks!
Post by Larry Serflaten
Post by Dan Johnson
We're using the Webbrowser control to display html help documents
(stored
Post by Larry Serflaten
Post by Dan Johnson
locally) to our users. We need to be able to access the body text of
the
Post by Larry Serflaten
Post by Dan Johnson
html document both for purposes of editing the text and searching the
text.
Post by Larry Serflaten
Post by Dan Johnson
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not
necessarily
Post by Larry Serflaten
Post by Dan Johnson
have to open/search the html file using the Webbrowser control BTW.
This may help you get a little farther along;
On a new form add a webbrowser, a textbox and button for the address
bar, and 2 option buttons to select the output (all using default names).
HTH
LFS
Private Sub Form_Load()
Command1.Caption = "Go"
Option1.Caption = "HTML"
Option2.Caption = "Text"
Option2.Value = True
Text1.Text = "www.google.com"
End Sub
Private Sub Command1_Click()
WebBrowser1.Navigate2 Text1.Text
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As
Variant)
Post by Larry Serflaten
If pDisp = WebBrowser1 Then
If Option1 Then
Debug.Print WebBrowser1.Document.documentElement.outerHTML
Else
Debug.Print WebBrowser1.Document.documentElement.innerText
End If
End If
End Sub
David J Mark
2006-01-23 13:30:17 UTC
Permalink
+
Post by mayayana
It's a pain in the neck, due to the complexity of
the IE DOM and the imprecision of HTML.
The DOM is not specific to IE and HTML is quite precise. The problem with
VB DHTML editors is that they all use the same crappy control (DHTMLEdit),
which outputs lousy HTML.
Post by mayayana
(If it really worked without problems then WYSIWYG
webpage editors would be easy to write and
would be actually useful.)
The problem is that they are too easy to write with the above-mentioned
control, so there is a glut of lousy examples out there (which all output
the same "alphabet soup" HTML.)
Post by mayayana
On the other hand, there is an amazing amount that you
can do with the IE DOM in a WB. If you set a reference
to MSHTML you'll get intellisense for the whole thing.
This example might be somewhat useful. It's a
WYSIWYG HTML editor written in VBScript - running
in an IE window - for the purpose of demonstrating
DOM functions and properties.
http://www.jsware.net/jsware/scripts.php3#domed
Doing it in VB is almost identical. It just
uses defined DOM objects through the MSHTML
type library, while script doesn't define variables
by type. The script sample might give you what you
want in terms of working out the methods you
need to accomplish what you want to do dynamically
in the page.
--
(Remove Xs for return email.)
Post by Dan Johnson
Thank you Ted and Larry.
I actually need to use elements of both of your ideas. Since I want the
WebBrowser control to be the user interface, I need to open a text file,
make necessary edits to the text, save the revised text, then reopen the
saved file via WebBrowser1.Navigate2. This allows me to customize the
terminology used in the document.
I also need to study the WebBrowser1.Document.documentElement.innerText
property. I hadn't seen this before, and think I might be able to use it
to
Post by Dan Johnson
save a step from above.
"Take a ride in the [Color] car."
I want to run Replace(MyText, "[Color]", "Blue") and transfer the revised
text to the WebBrowser control, but NOT have the changes saved to the
original document since the [Color] variable might change over time. Is
the
Post by Dan Johnson
WebBrowser1.Document.documentElement.outerHTML property writable so I can
open the *.html document using WebBrowser.Navigate2 then merely rewrite
the
Post by Dan Johnson
outerHTML property? I will need to play with this.
Again, Thanks!
Post by Larry Serflaten
Post by Dan Johnson
We're using the Webbrowser control to display html help documents
(stored
Post by Larry Serflaten
Post by Dan Johnson
locally) to our users. We need to be able to access the body text of
the
Post by Larry Serflaten
Post by Dan Johnson
html document both for purposes of editing the text and searching the
text.
Post by Larry Serflaten
Post by Dan Johnson
I started palying with the Webbrowser.Document property, but I'm
getting
Post by Dan Johnson
Post by Larry Serflaten
Post by Dan Johnson
nowhere fast. Can anybody give me reference here? We do not
necessarily
Post by Larry Serflaten
Post by Dan Johnson
have to open/search the html file using the Webbrowser control BTW.
This may help you get a little farther along;
On a new form add a webbrowser, a textbox and button for the address
bar, and 2 option buttons to select the output (all using default
names).
Post by Dan Johnson
Post by Larry Serflaten
HTH
LFS
Private Sub Form_Load()
Command1.Caption = "Go"
Option1.Caption = "HTML"
Option2.Caption = "Text"
Option2.Value = True
Text1.Text = "www.google.com"
End Sub
Private Sub Command1_Click()
WebBrowser1.Navigate2 Text1.Text
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As
Variant)
Post by Larry Serflaten
If pDisp = WebBrowser1 Then
If Option1 Then
Debug.Print WebBrowser1.Document.documentElement.outerHTML
Else
Debug.Print WebBrowser1.Document.documentElement.innerText
End If
End If
End Sub
Dan Johnson
2006-01-23 14:24:52 UTC
Permalink
Post by mayayana
This example might be somewhat useful. It's a
WYSIWYG HTML editor written in VBScript - running
in an IE window - for the purpose of demonstrating
DOM functions and properties.
http://www.jsware.net/jsware/scripts.php3#domed
Will take a look at this.
Larry Serflaten
2006-01-22 19:18:22 UTC
Permalink
Post by Dan Johnson
I want to run Replace(MyText, "[Color]", "Blue") and transfer the revised
text to the WebBrowser control, but NOT have the changes saved to the
original document since the [Color] variable might change over time. Is the
WebBrowser1.Document.documentElement.outerHTML property writable so I can
open the *.html document using WebBrowser.Navigate2 then merely rewrite the
outerHTML property? I will need to play with this.
Add a reference to Microsoft HTML Object Library. With that, you'd be able to
read the HTML file into a string, do your replacing of words, etc. and then write it
out to the control. Reading the file would use the standard Open and Close methods,
and writing it out to the control after you do the replacements would go something
like this:


Dim IDoc As IHTMLDocument

Set IDoc = WebBrowser1.Document
IDoc.open "text/html", "replace"
IDoc.write NewValue ' (The whole HTML file)
IDoc.Close

To initialize the browser navigate to "about:blank" at the start of your app
to let it set up the DOM. (document property).

LFS
nrford
2006-01-23 02:09:37 UTC
Permalink
Post by Dan Johnson
"Take a ride in the [Color] car."
I want to run Replace(MyText, "[Color]", "Blue") and transfer the revised
text to the WebBrowser control, but NOT have the changes saved to the
original document since the [Color] variable might change over time....
Can't you save either the original file or the revised file
to a different file name and display the revised file?
Dan Johnson
2006-01-23 14:19:30 UTC
Permalink
Post by nrford
Can't you save either the original file or the revised file
to a different file name and display the revised file?
That's actually the plan right now.
nrford
2006-01-23 02:14:22 UTC
Permalink
Post by Dan Johnson
We're using the Webbrowser control to display html help documents (stored
locally) to our users. We need to be able to access the body text of the
html document both for purposes of editing the text and searching the text.
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not necessarily
have to open/search the html file using the Webbrowser control BTW.
FWIW, I have an html file viewer/editor that I use
for displaying html help documents stored locally or
online.

The editor has search-and-replace, as well as a
spelling checker, and some features specific to
my program's needs which would have to be
changed for generic use.

I've uploaded the source to
http://www.cardsharkgames.com/fileviewer.zip
but I didn't include the spelling dictionary because
it's 4MB. If interested, I can upload it.
David J Mark
2006-01-23 13:24:08 UTC
Permalink
Post by Dan Johnson
We're using the Webbrowser control to display html help documents (stored
Why? Use the HTML Help API. You can launch any topic you want, open the
search window, do What's This? help, etc. Why try to re-write all of that
with the WebBrowser?
Post by Dan Johnson
locally) to our users. We need to be able to access the body text of the
html document both for purposes of editing the text and searching the text.
I started palying with the Webbrowser.Document property, but I'm getting
nowhere fast. Can anybody give me reference here? We do not necessarily
have to open/search the html file using the Webbrowser control BTW.
Dan Johnson
2006-01-23 14:18:22 UTC
Permalink
Post by David J Mark
Why? Use the HTML Help API. You can launch any topic you want, open the
search window, do What's This? help, etc. Why try to re-write all of that
with the WebBrowser?
Two reasons really.

Different versions of the same product require different language in the
help file. Maybe we could ship three different chm files, BUT...

Within the same version, the terminology used varies depending upon
user-selected terminology. It seems to us that we'd like to be able to
customize the help file using language the user is familiar with (ie,
language that they've chosen to use).

You may still be right, but at a minimum I want to explore using a more
customized version of a help file.

Dan
nrford
2006-01-23 23:14:58 UTC
Permalink
Post by Dan Johnson
Post by David J Mark
Why? Use the HTML Help API. You can launch any topic you want, open the
search window, do What's This? help, etc. Why try to re-write all of that
with the WebBrowser?
Two reasons really.
Different versions of the same product require different language in the
help file. Maybe we could ship three different chm files, BUT...
Within the same version, the terminology used varies depending upon
user-selected terminology. It seems to us that we'd like to be able to
customize the help file using language the user is familiar with (ie,
language that they've chosen to use).
You may still be right, but at a minimum I want to explore using a more
customized version of a help file.
In my project, I use the html file viewer/editor because
it allows the user to edit or create additional reference
files related to the program -- probably not a factor for
most projects, but a nice feature for mine.

Loading...