Solved

Problem with axwebbrowser control - cant access innerhtml or any elements

Posted on 2004-04-19
7
1,576 Views
Last Modified: 2008-03-06
I have an axwebbrowser control in a form.  I navigate to a page.  I am interested in getting the html from that page into a string variable.  For some reason, I cant use mypagedata = browser.Document.body.innerHTML like i would if it was a SHDocVw.InternetExplorer object.  In fact, any attempt to access similar elements always seem to result in the system crashing - not only that but I cant even get an error message most of the time (no source code available for current location).

Looking for a solution to get this data into a variable.  Also any documentation this object might be helpful.
0
Comment
Question by:krose777
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:M_o_n_t_y
ID: 10882444
Hi KRose,

This worked for me:


Private Sub cmdAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAnalyze.Click

        Dim oDoc As mshtml.HTMLDocument = AxWebBrowser1.Document
        MsgBox(oDoc.body.innerHTML)

End Sub

Let me know if that doesn't work for you...

                            -Monty
0
 
LVL 1

Expert Comment

by:M_o_n_t_y
ID: 10882489
Oh yeah, I needed a reference to the Microsoft.mshtml .Net assembly (Under Project --> Add reference --> .Net tab).

                    -Monty
0
 
LVL 2

Author Comment

by:krose777
ID: 10884155
i get a "null reference exception".  If i inspect the object in a watch window and type in odoc.body.innerhtml, I get "referenced object Body has a value of nothing".  Why would this be the case?  

Other than that, i like the looks of this solution - if I can get it to work.  I like that the document methods/properties/etc are exposed, whereas whenever im accessing similar elements with an internet explorer com object its always late binding and the debugger doesnt check for spelling errors, etc.  Im assuming i can use this as a wrapper for SHDocVw.InternetExplorer too?  I will experiment.  
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:M_o_n_t_y
ID: 10884258
Sorry, you've got to load the document first and wait until the DocumentLoaded event fires on the browser. This code should be a little clearer: (create two buttons, one called cmdLoad and one called cmdAnalyze):


    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
        cmdAnalyze.Enabled = False
        AxWebBrowser1.Navigate("http://www.experts-exchange.com")
    End Sub

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        cmdAnalyze.Enabled = True
    End Sub

    Private Sub cmdAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAnalyze.Click
        Dim oDoc As mshtml.HTMLDocument = AxWebBrowser1.Document
        MsgBox(oDoc.body.innerHTML)
    End Sub
0
 
LVL 2

Author Comment

by:krose777
ID: 10928758
cant get that bit of code to work.  The documentcomplete event never fires.  Wont:

                While Browser.Busy
                    Windows.Forms.Application.DoEvents()
                End While

or some other browser.property work ?
0
 
LVL 1

Accepted Solution

by:
M_o_n_t_y earned 500 total points
ID: 10929024
OK, create a new form called "test", open the code, clear it all out and replace it with this. (Make sure you have a reference to the Microsoft.mshtml .Net assembly). This definitely does work:

Public Class Test
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
    Friend WithEvents cmdLoad As System.Windows.Forms.Button
    Friend WithEvents cmdAnalyze As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Test))
        Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser
        Me.cmdLoad = New System.Windows.Forms.Button
        Me.cmdAnalyze = New System.Windows.Forms.Button
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'AxWebBrowser1
        '
        Me.AxWebBrowser1.Enabled = True
        Me.AxWebBrowser1.Location = New System.Drawing.Point(8, 8)
        Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser1.Size = New System.Drawing.Size(440, 328)
        Me.AxWebBrowser1.TabIndex = 5
        '
        'cmdLoad
        '
        Me.cmdLoad.Location = New System.Drawing.Point(264, 344)
        Me.cmdLoad.Name = "cmdLoad"
        Me.cmdLoad.Size = New System.Drawing.Size(88, 24)
        Me.cmdLoad.TabIndex = 6
        Me.cmdLoad.Text = "Load"
        '
        'cmdAnalyze
        '
        Me.cmdAnalyze.Location = New System.Drawing.Point(360, 344)
        Me.cmdAnalyze.Name = "cmdAnalyze"
        Me.cmdAnalyze.Size = New System.Drawing.Size(88, 24)
        Me.cmdAnalyze.TabIndex = 7
        Me.cmdAnalyze.Text = "Analyze"
        '
        'Test
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 382)
        Me.Controls.Add(Me.cmdAnalyze)
        Me.Controls.Add(Me.cmdLoad)
        Me.Controls.Add(Me.AxWebBrowser1)
        Me.Name = "Test"
        Me.Text = "Test"
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
        cmdAnalyze.Enabled = False
        AxWebBrowser1.Navigate("http://www.experts-exchange.com")
    End Sub

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        cmdAnalyze.Enabled = True
    End Sub

    Private Sub cmdAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAnalyze.Click
        Dim oDoc As mshtml.HTMLDocument = AxWebBrowser1.Document
        MsgBox(oDoc.body.innerHTML)
    End Sub

End Class
0
 
LVL 2

Author Comment

by:krose777
ID: 10965638
Monty -
I dont know why but i could never get yours to work properly - I was experimenting and this seems to work for some unknown reason:

 While Browser.Busy Or Not (Browser.ReadyState = SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                    Windows.Forms.Application.DoEvents()
 End While

i was just checking for browser.busy, but that was unreliable for some reason.  Thank you for the help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get column names from table in vb.net 8 51
Create a datatable in vb.net dynamically 1 22
VB.NET Inline If statement 4 15
Data organization issue 7 16
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question