Grab all links on the page
This code shows how to grab and list all the links on a page, this can be used a spider software for a search engine site.
In order to get this code to load you must add the "Microsoft HTML Object Library" into your app refrences.
VB Code:
Option Explicit
Private Sub Form_Load()
WebBrowser1.Navigate "www.vbforums.com"
End Sub
Private Sub WebBrowser1_DownloadComplete()
'you must add the "Microsoft HTML Object Library"!!!!!!!!!
Dim HTMLdoc As HTMLDocument
Dim HTMLlinks As HTMLAnchorElement
Dim STRtxt As String
' List the links.
On Error Resume Next
Set HTMLdoc = WebBrowser1.Document
For Each HTMLlinks In HTMLdoc.links
STRtxt = STRtxt & HTMLlinks.href & vbCrLf
Next HTMLlinks
Text1.Text = STRtxt
End Sub
You can add this code in order to log this files.
VB Code:
Open "C:\Documents and Settings\[YOU USERNAME]\Desktop\link log.txt" For Append As #1
Print #1, STRtxt
Close #1
Save Page
This code shows you how to save the browser's page.
VB Code:
Option Explicit
Private Sub Command1_Click()
WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate2 "www.google.com"
End Sub
Open Page
Here is how to load a webpage into the webbrowser.
VB Code:
Private Sub Command2_Click()
WebBrowser1.ExecWB OLECMDID_OPEN, OLECMDEXECOPT_PROMPTUSER
End Sub
This is how to open a page, using the comman dialog's way
VB Code:
Option Explicit
Private Sub Command1_Click()
On Error Resume Next
With CommonDialog1
.DialogTitle = "Open File"
.Filter = "Web page (*.htm;*.html) | *.htm;*.html|" & _
"All Supported Picture formats|*.gif;*.tif;*.pcd;*.jpg;*.wmf;" & _
"*.tga;*.jpeg;*.ras;*.png;*.eps;*.bmp;*.pcx|" & _
"Text formats (*.txt;*.doc)|*.txt;*.doc|" & _
"All files (*.*)|*.*|"
.ShowOpen
.Flags = 5
WebBrowser1.Navigate2 .FileName
End With
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate2 "www.google.com"
End Sub
Auto Submit
This Simple Code I Made To show you how to make a submittion form. This code will autofill the need filled and submit it.
VB Code:
Private Sub Command1_Click()
Dim strwebsite As String
Dim stremail As String
strwebsite = "http://www.mysite.com"
WebBrowser1.Document.addurl.URL.Value = strwebsite
WebBrowser1.Document.addurl.Email.Value = stremail
WebBrowser1.Document.addurl.Submit
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate "http://www.scrubtheweb.com/addurl.html"
End Sub
Using A ProgressBar With The Webbrowser
This is to show how to use a progressbar with a webbrowser control.
VB Code:
Private Sub Form_Load()
WebBrowser1.Navigate "www.msn.com"
ProgressBar1.Appearance = ccFlat
ProgressBar1.Scrolling = ccScrollingSmooth
End Sub
Private Sub WebBrowser1_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)
On Error Resume Next
If Progress = -1 Then ProgressBar1.Value = 100
Me.Caption = "100%"
If Progress > 0 And ProgressMax > 0 Then
ProgressBar1.Value = Progress * 100 / ProgressMax
Me.Caption = Int(Progress * 100 / ProgressMax) & "%"
End If
Exit Sub
End Sub
Setting a Control in a Webbrowser to focus
This shows how to set a control inside the webbrowser into focus.
VB Code:
Private Sub Command1_Click()
WebBrowser1.Document.All("q").focus 'Set the search text filed in focus
End Sub
Private Sub Command2_Click()
WebBrowser1.Document.All("btnI").focus 'Set the google "I Am feeling lucky in focus button"
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate "http://www.google.com/"
End Sub
Or you can use:
VB Code:
WebBrowser1.Document.getElementById("Object's Name").Focus
Checkbox in a page, how to control it
This is an example on how to check or uncheck the remember me checkbox on the google login page:
VB Code:
Private Sub Form_Load()
WebBrowser1.Navigate "https://www.google.com/accounts/ManageAccount"
End Sub
Private Sub Check1_Click()
If Check1.Value = 0 Then
WebBrowser1.Document.All.PersistentCookie.Checked = False 'unchecked
Else
WebBrowser1.Document.All.PersistentCookie.Checked = True 'checked
End If
End Sub
Or
VB Code:
Private Sub Form_Load()
WebBrowser1.Navigate "https://www.google.com/accounts/ManageAccount"
End Sub
Private Sub Check1_Click()
If Check1.Value = 0 Then
WebBrowser1.Document.All.PersistentCookie.Checked = 0 'unchecked
Else
WebBrowser1.Document.All.PersistentCookie.Checked = 1 'checked
End If
End Sub
Or
VB Code:
Private Sub Form_Load()
WebBrowser1.Navigate "https://www.google.com/accounts/ManageAccount"
End Sub
Private Sub Check1_Click()
If Check1.Value = 0 Then
WebBrowser1.Document.getElementById("PersistentCookie").Checked = False 'unchecked
Else
WebBrowser1.Document.getElementById("PersistentCookie").Checked = True 'checked
End If
End Sub
Custom Right Click Menu
This is an example show how to make your own custom right click menu. in order for this to work you must add the "Must Add Microsoft HTML Object Library" to your refrance.Also make your own custom menu using the menu editor I named my "mnu"
Please Note it will effect all the context menus in the webbrowser.
VB Code:
'Must Add Microsoft HTML Object Library
Option Explicit
Public WithEvents HTML As HTMLDocument
Private Function HTML_oncontextmenu() As Boolean
HTML_oncontextmenu = False
PopupMenu mnu '<---Check the mnu to your own menu name
End Function
Private Sub Form_Load()
WebBrowser1.Navigate "www.google.com"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set HTML = Nothing
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, _
URL As Variant)
Set HTML = WebBrowser1.Document
End Sub