Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with axwebbrowser control - cant access innerhtml or any elements

Posted on 2004-04-19
7
Medium Priority
?
1,586 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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