Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > Save all Links in Webpage

Thread Tools Display Modes
Old 04-11-2012, 01:10 AM
TechUpdates TechUpdates is offline
Join Date: Apr 2012
Posts: 1
Unhappy Save all Links in Webpage

Hello Friends,
I am a complete newbie in this programing language. The thing that I am trying to achieve is -
1) Create a simple Web Browser [DONE]
2) Clear Cookies by Mouse Click on the Button [DONE]
3) Save the links present in the webpage that the user is viewing through my browser [PROBLEM]

Here's the code that I have done -
Imports System.IO
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using MyWriter As New StreamWriter("C:\Documents and Settings\Administrator\Desktop\test.txt")
            'Declare your StreamWriter and put the path to the file that you want to write as a Parameter of the StreamWriter. If the File does not exist, then it will be created. If the File Exists it will be overwritten. (You can add the Append Boolean to the Parameters of the StreamWriter if you don't want the File overwritten. StreamWriter(PathToFile, AppendBoolean))

            Dim MyString As String = WebBrowser1.Document.Body.InnerText ' Declare your String and set the Value of the String to be the InnerText of the document in the WebBrowser Control

            MyWriter.Write(MyString) ' Write the String to the File
            MyWriter.Flush() ' Clear the buffers for the current writer and causes any buffered data to be written to the underlying stream
            MyWriter.Close() ' Close the StreamWriter
        End Using

    End Sub
But when I try to debug this I get this error -
A first chance exception of type 'System.NullReferenceException' occurred in MyWebBrowser.exe
and this line gets highlighted in yellow - MyString As String = WebBrowser1.Document.Body.InnerText

Please help!!
Reply With Quote
Old 04-11-2012, 03:30 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403

The webbrowser is still navigating to the site when it hits that line of code. What you need to do is wait for the document to finish loading and then you can get to the Document of the WebBrowser.

For that purpose the WebBrowser control has the DocumentCompleted event. It's worth noting that a single webpage can have multiple frames and each of those frames will fire a DocumentCompleted event so it's not possible to know how many times the event will fire. Fortunately it's easy enough to check that that whole page has completed by checking the Url of the event to the Url of the webbrowser...
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If e.Url.Equals(WebBrowser1.Url) Then

        ' You can do your Webbrowser1.Document work here.

    End if
End Sub
There are no computers in heaven!
Reply With Quote

link error, webbrowser

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..