Obtain the source code of the webbrowser control webpage (favorites)

Source: Internet
Author: User

I found on the Internet that using the rft control to save webbrowse text txthtml is RichTextBox
Txthtml. Text = webbrowser1.document. Body. innertext
'Flag: rsftext is saved as a TXT file, and the strtmp file path
Txthtml. SaveFile strtmp, rtftext

Set its name attribute to Web

Private sub commandementclick ()
Web. navigate "www.google.com"
End sub

Private sub web_documentcomplete (byval Pdisp as object, URL as variant)
Set Doc = web. Document
For each I in Doc. All
Msgbox typename (I)
Text1.text = text1.text & vbclrf & I. innertext
Next
End sub

========================================================== ========================================================== ==============
Reprinted

'Reference Microsoft HTML Object Library

Dim odoc as htmldocument
Dim oelement as object
Dim otxtrgn as object
Dim sselectedtext as string

Set odoc = webbrowser1.document 'to get the Document Object
Set oelement = odoc. getelementbyid ("T1") 'to get the Object ID = "T1"
Set otxtrgn = odoc. selection. createRange 'get the currently selected region object of the document.

Sselectedtext = otxtrgn. text' select region text assignment

Oelement. Focus 'T1 "Object obtains focus

Oelement. select 'all selected objects "T1"

Debug. Print "you selected text:" & sselectedtext

Other functions are provided in the above section. The following is simplified:
Dim odoc as object
Dim otxtrgn as object
Dim sselectedhtml as string

Set odoc = webbrowser1.document 'to get the Document Object
Set otxtrgn = odoc. selection. createRange 'get the currently selected region object of the document.

Sselectedhtml = otxtrgn.html text 'select region text assignment

Text1.text = sselectedhtml 'text box display capture HTML source code
... 'Or continue to analyze the source code.

========================================================== ========================================================== ============================

I used webbrowser to get the webpage source code and directly run normally, but an error occurred after compilation.
Private sub commandementclick ()
Webbrowser1.navigate "http://www.sdqx.gov.cn/sdcity.php"
End sub

Private sub webbrowser1_downloadcomplete ()
'Page download complete
Dim doc, objhtml
Set Doc = webbrowser1.document

Set objhtml = Doc. Body. createTextRange ()
If not isnull (objhtml) then
Text1.text = objhtml.html text
End if

End sub

I used webbrowser to get the webpage source code and directly run normally, but an error occurred after compilation.

Tip: Real-time error "91" object variable or with block variable is not set
It may be because the download is not complete,

Private sub webbrowser1_downloadcomplete ()
If webbrowser. Busy = false then
Dim doc, objhtml
Set Doc = webbrowser1.document

Set objhtml = Doc. Body. createTextRange ()
If not isnull (objhtml) then
Text1.text = objhtml.html text
End if
End if
End sub

It is better to use XMLHTTP for the source code of Your webpage.

Reference MSXML first

Dim X as new msxml2.xmlhttp
X. Open "get", "http://www.sina.com", false
X. Send

Msgbox strconv (X. responsebody, vbunicode)

 

========================================================== ========================================================== ====================
I found on the Internet that using the rft control to save webbrowse text txthtml is RichTextBox
Txthtml. Text = webbrowser1.document. Body. innertext
'Flag: rsftext is saved as a TXT file, and the strtmp file path
Txthtml. SaveFile strtmp, rtftext

========================================================== ========================================================== =====

Private sub webbrowser1_downloadcomplete ()
Dim objhtml as object
'Link finished is displayed in the status bar when the download is complete"
Set objhtml = me. webbrowser1.document. Body. createTextRange ()
If not isnull (objhtml) then
Text1.text = objhtml.html text
End if
End sub
Use the inet Control
Source1 = inet1.openurl ("www.csdn.net ")
If source1 <> "" then
Richtextbox1.text = source1
Me. inet1.cancel
Else
Source = msgbox ("source code is not available.", vbinformation, "source code ")
End if

Private sub commandementclick ()
Text1.text = webbrowser1.document. Body. innerhtml
End sub

========================================================== ========================================================== =
Add timer, commandbutton, text
Private sub commandementclick ()
Webbrowser1.navigate the http://www.sohu.com/
Timer1.enabled = true
End sub

Private sub timer1_timer ()
Dim doc, objhtml as object
Dim I as integer
Dim strhtml as string

If not webbrowser1.busy then
Set doc=webbrowser1.doc ument
I = 0
Set objhtml = Doc. Body. createTextRange ()
If not isnull (objhtml) then
Text1.text1_objhtml.html text
End if
Timer1.enabled = false
End if
End sub

Dim doc, objhtml as object
If not webbrowser1.busy then
Set Doc = webbrowser1.document
Set objhtml = Doc. Body. createTextRange ()
If not isnull (objhtml) then
Text1.text1_objhtml.html text
End if
Set Doc = nothing
Set objhtml = nothing

End if

========================================================== ========================================================== ==============================
You can also try internetreadfile:
Option explicit

Private declare function internetopen lib "wininet. dll" alias "internetopena "(_
Byval sagent as string, byval laccesstype as long ,_
Byval sproxyname as string, byval sproxybypass as string ,_
Byval lflags as long) as long
Private declare function internetopenurl lib "wininet. dll" alias "internetopenurla "(_
Byval hinternetsession as long, byval Surl as string ,_
Byval sheaders as string, byval lheaderslength as long ,_
Byval lflags as long, byval lcontext as long) as long
Private declare function internetreadfile lib "wininet. dll "(_
Byval hfile as long, byval sbuffer as string ,_
Byval lnumbytestoread as long ,_
Lnumberofbytesread as long) as integer
Private declare function internetclose

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.