How to copy all the text display in AxWebBrowser?


  What I want to know is after I called this function.  web.Navigate2(txtURL.Text).  How do I know?

1. When the loading of the page is completed ?
2. How to copy all the text display in AxWebBrowser control and save to database? (All done automatically by program)  It is better that the AxWebBrowser is declare as variable to do this task instead of draw it on vb form.

  I tried to use the code below but my cant find those class and functions.
(For question 2.)
Dim objRange as IHTMLTxtRange
set objRange = ActveDocument.body.createTextRange

  Thanks first for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello there,

'First, Make sure you declare these, brWeb is the name of my AxWebBrowser component
Private WithEvents doc As SHDocVw.DWebBrowserEvents_Event
Dim b As Object = brWeb.Application
doc = DirectCast(b, SHDocVw.WebBrowser_V1)

'This will navigate to the page

'This will pause your code right here in the form until the page that you declared in navigate2 is done loading

'My suggestion here is to save the html to a local text could also just save it to a string instead

 Dim oDoc As mshtml.HTMLDocument = brWeb.Document
 Dim MyHtml = oDoc.body.innerHTML

'Now from here you have to remove tags and whatnot to clean up the html for reading. This can get pretty complex if
'you have an extremely dynamic page.

'Search for the opening and closing tags you want

Start = txtFile.Text.IndexOf("<H4>")
Stop = txtFile.Text.IndexOf("</H4>")

'Then Just put them together

MyValue = MyHtml .Substring(start, stop)

So this is the basic idea on how to do all the stuff that you need. Searching for tags gets kinda crazy, if you are having a really tough time, find some code that converts html to xml, since its a lot easier to deal with. let me know if you need any help.
yongyihAuthor Commented:
Hi juravich

  Thanks for your help.  I think you have answered my first question.

  For my second question.  It is very difficult to clean up all tags in html file for certain web site.  I think the safest and easiest way is to copy all the text display in the browser and save it to a file.  You can try to go to any web site, press Ctrl+A, Ctrl+C and then paste it to a text file.
That wil be what I want from the html file.  I want this to be done by program automatically.

  I hope it is possible to do that in ^_^  Any idea about this code that I get from a web site.

Dim objRange as IHTMLTxtRange
set objRange = ActveDocument.body.createTextRange


hey there,

Sorry about that I misread your question.

The solution to the problem you have is rather simple actually.

Simply Change this

Dim oDoc As mshtml.HTMLDocument = brWeb.Document
Dim MyHtml = oDoc.body.innerHTML


Dim oDoc As mshtml.HTMLDocument = brWeb.Document
Dim objStreamWriter As StreamWriter
objStreamWriter = New StreamWriter("C:\Test.txt")


This code will take the text of the webpage and save it to a text file located at "C:\Test.txt", let me know if you need anymore help

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

make sure you have this

Imports System.IO

at the top of our code
yongyihAuthor Commented:
Hi juravich,

  Thank You.  That is what I want.  One more thing I need to do is add the reference.  "Add reference->.Net

  Currently the AxWebBrowser() is dispay in my form.  Is it possible to declare it as a variable in a function, then call navigate2 and get the body text ?

  Thanks again for your help.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.