borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (ADO Connection) New Topic New Poll Post Reply
AndreaVB Forum : Frequently Asked Questions : Web Browser control
Poster Message
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21

icon Web Browser control

Web browser control

Since I have been seen lately many people asking questions about the webbrowser control.

First of all the webbrowser control is not one of the controls that come by default in the control box. All you have to do to add it there is to press Ctrl + T key or (Project -> Components using the menu). Once in the components select window scroll down and check the box next to “Microsoft Internet Controls” and click ok.

Contents:
Navigating to a site
Popup browser using your own form
Check if word/string is found on the page
Making page on startup
Regular Browser Functions
Advanced browser functions
Changing web browser’s Font Size
Disabling functions appropriately (Back/Forward)
Disabling functions appropriately (page setup/print preview/print setup)
Removing Right Click Menu From the browser control
Grab all links on the page
Save Page
Open Page
Auto Submit
Using A ProgressBar With The Webbrowser
Setting a Control in a Webbrowser to focus
Checkbox in a page, how to control it
Custom Right Click Menu

____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:25 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

Navigating to a site:
Navigating to a site...
visual basic code:
WebBrowser1.Navigate "www.google.com"


Opening a popup window with your app
If the user go to a site where a new page needs to come in a new browser this code is made to open the target site in a new form.
visual basic code:
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
Dim frm As Form1
Set frm = New Form1
Set ppDisp = frm.WebBrowser1.Object
frm.Show
End Sub


____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:28 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

Check if word/string is found on the page
This code will let you check the page for a word or a sentence.
visual basic code:
Private Sub Command1_Click()
    Dim strfindword As String
        strfindword = InputBox("What are you looking for?", "Find", "") ' what word to find?
            If WebPageContains(strfindword) = True Then 'check if the word is in page
                MsgBox "The webpage contains the text" 'string is in page
            Else
                MsgBox "The webpage doesn't contains the text" 'string is not in page
            End If
End Sub
Private Function WebPageContains(ByVal s As String) As Boolean
    Dim i As Long, EHTML
    For i = 1 To WebBrowser1.Document.All.length
        Set EHTML = _
        WebBrowser1.Document.All.Item(i)


        If Not (EHTML Is Nothing) Then
            If InStr(1, EHTML.innerHTML, _
            s, vbTextCompare) > 0 Then
            WebPageContains = True
            Exit Function
        End If
    End If
Next i
End Function
Private Sub Form_Load()
    WebBrowser1.Navigate2 "www.msn.com"
End Sub


____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:32 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

Making page on startup
This code show you how to make a page and show it on event.
visual basic code:
Private Sub Form_Load()
    WebBrowser1.Navigate "about:blank"
End Sub

Private Sub Command1_Click()
    Dim HTML As String
        '----------The HTML CODE GOES FROM HERE AND DOWN----------
    HTML = "<HTML>" & _
            "<TITLE>Page On Load</TITLE>" & _
            "<BODY>" & _
            "<FONT COLOR = BLUE>" & _
            "This is a " & _
            "<FONT SIZE = 5>" & _
            "<B>" & _
            "programmatically " & _
            "</B>" & _
            "</FONT SIZE>" & _
            "made page" & _
            "</FONT>" & _
            "</BODY>" & _
            "</HTML>"
            '----------The HTML CODE GOES HERE AND ABOVE----------
    WebBrowser1.Document.Write HTML
End Sub


____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:38 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

Regular Browser Functions
This are the basic internet browser's functions.
visual basic code:
Private Sub Command1_Click(Index As Integer)
On Error Resume Next ' just in case there is no page back or forward
                    'I showed how to disabel them if you scroll down more
    Select Case Index
        Case 0 'Go Back Button
            WebBrowser1.GoBack 'Go Back one Page
        Case 1 'Go Forward Button
            WebBrowser1.GoForward 'Go Forward one Page
        Case 2 'Stop Button
            WebBrowser1.Stop 'stop page
        Case 3 'Refresh Button
            WebBrowser1.Refresh 'refresh page
        Case 4 'Go Home Button
            WebBrowser1.GoHome 'Go to home page
        Case 5 'Search Button
            WebBrowser1.GoSearch 'Search
    End Select
End Sub


Advanced Browser Functions
This are the more complex browser functions like print and page Properties.
visual basic code:
Private Sub Command1_Click() 'Print Button
    WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT 'Show Print Window
End Sub
Private Sub Command2_Click() 'Print Preview Button
    WebBrowser1.ExecWB OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT 'Show Print Preview Window
End Sub
Private Sub Command3_Click() 'Page Setup Button
    WebBrowser1.ExecWB OLECMDID_PAGESETUP, OLECMDEXECOPT_DODEFAULT 'Show Page Setup Window
End Sub
Private Sub Command4_Click() 'Page Properties Button
    WebBrowser1.ExecWB OLECMDID_PROPERTIES, OLECMDEXECOPT_DODEFAULT 'Show Page Properties Window
End Sub
Private Sub Form_Load()
    WebBrowser1.Navigate "www.google.com"
End Sub


____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:40 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

Changing web browser&#8217;s Font Size
This Code shows you how to change the page font size, just like internet explorer View -> Text Size Menu
visual basic code:
Private Sub Command1_Click() ' Smallest Button
On Error Resume Next
    WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(0), vbNull
End Sub
Private Sub Command2_Click() 'Small Button
On Error Resume Next
    WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(1), vbNull
End Sub
Private Sub Command3_Click() 'Medium Button
On Error Resume Next
    WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(2), vbNull
End Sub
Private Sub Command4_Click() 'Large Button
On Error Resume Next
    WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(3), vbNull
End Sub
Private Sub Command5_Click() 'Largest Button
On Error Resume Next
    WebBrowser1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(4), vbNull
End Sub
Private Sub Form_Load()
    WebBrowser1.Navigate2 "www.google.com"
End Sub


Disabling functions appropriately (Back/Forward)
This code shows you how to appropriately disable the back and forward button.
visual basic code:
Private Sub Command1_Click() 'Go Back Button
    WebBrowser1.GoBack 'Go Back
End Sub
Private Sub Command2_Click() 'Go Forward Button
    WebBrowser1.GoForward 'Go Forward
End Sub
Private Sub Form_Load()
    WebBrowser1.Navigate "www.google.com"
End Sub
Private Sub WebBrowser1_CommandStateChange(ByVal Command As Long, ByVal Enable As Boolean)
    Select Case Command
        Case 1 'Forward
            Command2.Enabled = Enable
        Case 2 'Back
            Command1.Enabled = Enable
    End Select
End Sub

Disabling functions appropriately (page setup/print preview/print setup)
This code shows you how to appropriately disable the page setup or print setup.
visual basic code:
Private Sub Command1_Click() 'Print Button
    WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT 'Show Print Window
End Sub
Private Sub Command2_Click() 'Print Preview Button
    WebBrowser1.ExecWB OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT 'Show Print Preview Window
End Sub
Private Sub Command3_Click() 'Page Setup Button
    WebBrowser1.ExecWB OLECMDID_PAGESETUP, OLECMDEXECOPT_DODEFAULT 'Show Page Setup Window
End Sub
Private Sub Form_Load()
    WebBrowser1.Navigate "www.google.com"
End Sub
Public Function Enable_or_Disable()
    If WebBrowser1.QueryStatusWB(OLECMDID_PRINT) = 0 Then
        Command1.Enabled = False
    Else
        Command1.Enabled = True
    End If
    If WebBrowser1.QueryStatusWB(OLECMDID_PRINTPREVIEW) = 0 Then
        Command2.Enabled = False
    Else
        Command2.Enabled = True
    End If

    If WebBrowser1.QueryStatusWB(OLECMDID_PAGESETUP) = 0 Then
        Command3.Enabled = False
    Else
        Command3.Enabled = True
    End If
End Function
Private Sub WebBrowser1_BeforeNavigate2 _
                    (ByVal pDisp As Object, _
                                URL As Variant, _
                                Flags As Variant, _
                        TargetFrameName As Variant, _
                                PostData As Variant, _
                                    Headers As Variant, _
                                        Cancel As Boolean)
    Enable_or_Disable
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    Enable_or_Disable
End Sub


____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:44 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

Removing Right Click Menu From the browser control
So you need to remove the right click menu from the control, right? well there are more then two ways one of them, which i knew but i won't bother telling is using lots of hooking, waist of thime since all you have to do is download a simple helper file.
First you need to go to http://support.microsoft.com/kb/q183235/ and download the WBCustomizer.dll. once done go to the"Project Menu" and click on "Refrences" Click on browse and add the 'WBCustomizer.dll' to you app. once done just add this simple code.
visual basic code:
Option Explicit
    Dim CustomWB As WBCustomizer 'Deceler the CustomWB
Private Sub Form_Load()
   Set CustomWB = New WBCustomizer
   With CustomWB
      .EnableContextMenus = False 'Disable The Menu
      .EnableAllAccelerators = True
      
      Set .WebBrowser = WebBrowser1
   End With
    WebBrowser1.Navigate "www.google.com"
        CustomWB.EnableContextMenus = False
End Sub


____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:46 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

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.
visual basic 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.
visual basic 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.
visual basic 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.
visual basic 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
visual basic 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.
visual basic code:
Private Sub Command1_Click()
Dim strwebsite As String
    Dim stremail As String
        strwebsite = "http://www.mysite.com"
            stremail = "myemail@host.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.
visual basic 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.
visual basic 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:
visual basic 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:

visual basic 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

visual basic 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
visual basic 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.

visual basic 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

____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 03:51 AM
View Profile Send Email to User Show All Posts | Quote Reply
qwertypunk
Level: Big Cheese


Registered: 21-04-2006
Posts: 21
icon Re: Web Browser control

I hope u find this usefulif u have any problems u can question any time

____________________________
There are two ways to make error free programs...only the third one works.

24-04-2006 at 09:32 AM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Web Browser control

Nice work qwerty, if you don't mind i'll move it to FAQ's

Steve  

25-04-2006 at 08:55 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Frequently Asked Questions : Web Browser control
Next Topic (ADO Connection) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder