0
Answer

Grab links from a Web page using VB.NET?

Vamshi Krishna Vemula

Vamshi Krishna Vemula

14y
3.6k
1
Hi,

I need to grab all the links from a Web page and with Googling I got some code to do that but it fails and I get the error "Type 'HTMLAnchorElement' is not defined."  I have added "Microsoft HTML Object Library" reference to the project but still it gives same error.  I also referenced this library manually by browsing "C:\program files\microsoft.net\primary interop assemblies\Microsoft.mshtml.dll"


    Private Sub Form_Load()
        WebBrowser1.Navigate("www.vbforums.com")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        '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
        HTMLdoc = WebBrowser1.Document

        For Each HTMLlinks In HTMLdoc.Links
            STRtxt = STRtxt & HTMLlinks.href & vbCrLf
        Next (HTMLlinks)

        TextBox1.Text = STRtxt
    End Sub


Need help with this code.  Actually, my purpose is to identify a given link on a Web page and navigate it.  If this code works then I can modify it to my requirements or if there is any other way to find a given link on the page and click on it then it is much appreciated.

Thanks in advance.