Question

WebBrowser and MSHTML (working with HTML tables)

Asked by: adwooley2

Hello - hoping somebody can help me out here.

PROBLEM BACKGROUND: Created a web-browser with intent to take HTML innertext from HTML tables from websites and save the data to SQL Server. I want to use MSHTML so that I have fine-granular control over the HTML elements.  User will browse to a site and hit a download button which will with the HTML delivered to the WebBrowser control

PROBLEM:  Cannot seem to use mshtml.HTMLDocument correctly.  (I've made a reference to MSHTML)
My mshtml.HTMLDocument is declared as follows:  Dim doc As New mshtml.HTMLDocument
I can't cast WebBrowser.Document into mshtml.HTMLDocument and createDocumentFromUrl doesn't seem to work.  Think that I'm getting syntax wrong, but haven't found anything out there that fits the bill.

Can anybody out there help me instantiate my mshtml.HTMLDocument correctly? This is rather urgent.
Regards,
A.Wooley

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-09-28 at 20:46:34ID22007045
Topic

Microsoft Visual Basic.Net

Participating Experts
3
Points
500
Comments
16

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. MSHTML interfaces
    Hi ! I am working on a HTML editor and I would like to know if I can use the MSHTML interfaces in VB. For example what code should I use to access the IHTMLeditDesigner or the IHTMLCaret ? Thanks
  2. MSHTML - how to??
    Hello all, I've been searching for days on the Internet to find out how to suppress images and other content from downloading with the MSHTML library. I want to use this library to parse HTML pages, but can't figure out how to get just the HTML source, and not all the emb...
  3. MSHTML Editing
    Hello ! can anybody send me an example or tell me where to find one related to WYSIWYG MSHTML Editing. in particular it would be great if there was an example about how to edit multiple frames of a document at once. thanks !
  4. MSHTML Drop Down
    I'm creating a C# application that will launch a url in Internet Explorer using both the shdocvw and microsoft.mshtml references. After the page loads, I need to automatically enter data into form elements, then press the continue button on the screen. Currently, I'm able to ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: jaime_olivaresPosted on 2006-09-28 at 22:35:42ID: 17625326

You can do:

Dim browser as new WebBrowser
browser.DocumentText = "";  ' Initializes the browser control
Dim doc as HtmlDocument = browser.Document

Now doc points to a valid htmldocument
For more information have a look to: http://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser.document.aspx

 

 

by: adwooley2Posted on 2006-09-28 at 22:46:03ID: 17625357

Jaime, thanks for the reply.  But if you see my original question, I'm trying to grab a hold of mshtml.HTMLDocument.  I'm already able to get a valid HtmlDocuement (but the one from the WebBrowser is System.Windows.Forms.HtmlDocument, which is a different animal supposedly)

 

by: doobdavePosted on 2006-09-29 at 04:49:43ID: 17626528

which event of the browser object are you using when you assign the document to the variable?

 

by: adwooley2Posted on 2006-09-29 at 05:01:35ID: 17626579

Doobdave,
Actually, I just figured it out. My code looks like this:

        Dim objMSHTML As New mshtml.HTMLDocument
        Dim objDocument As mshtml.IHTMLDocument2
        Dim ips As IPersistStreamInit
       
        ips = DirectCast(objMSHTML, IPersistStreamInit)
        ips.InitNew()
        objDocument = objMSHTML.createDocumentFromUrl(Me.txtAddress.Text, vbNullString)
        Do Until objDocument.readyState = "complete"
            Application.DoEvents()
        Loop
        MsgBox(objDocument.body.innerHTML())
        Dim ObjDocument3 As mshtml.IHTMLDocument3
        ObjDocument3 = DirectCast(objDocument, mshtml.IHTMLDocument3)
       
That plus a lot of cryptic interface declarations at the top that I got off the web.
I appreciate your reply. Now I'm onto the next thing - how to make this app crawl over a website.  If you have any clue on that, I'm all ears.
Thanks.

A.Wooley
       

 

by: doobdavePosted on 2006-09-29 at 06:51:49ID: 17627242

What do you mean by "crawl over a website"?

 

by: adwooley2Posted on 2006-10-01 at 17:54:38ID: 17640306

Hi again.  What I mean by crawling over a website is examining each "A href" link, going to that particular link, then re-examining for more links.  It is a recursive loop that I'm after.  Make sense?

 

by: BeauTPosted on 2006-10-01 at 20:55:06ID: 17640883

Hello adwooley, I actually am in the very same boat as you. I also just recently figured out that the Interop interfaces need to be used in order to make the call to createDocument FromUrl. I did want to add a little to what you had here and also show how I would go about iteratign through all fo the anchors on a page.
First, I would recommend having these 4 pages from MSDN easily accessible as you are programming:

http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/document2/open.asp
http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/document3/document3.asp
http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/document4/ihtmldocument4.asp
http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/document5/ihtmldocument5.asp

Those are, respectively, the documentation for mshtml.IHTMLDocument2, mshtml.IHTMLDocument3, mshtml.IHTMLDocument4 and mshtml.IHTMLDocument5. Note that mshtml.IHTMLDocument is not really usable when you are trying to access the DOM; it provides no methods for accessing the DOM (see the documentation on it for additional information).

Also, since all of the interfaces hold references to the same object, you don't need to use DirectCast to make reference assignments, you can just assign them directly.

For browsing to other web pages, you want to use the open() method of mshtml.IHTMLDocument2.
open() can be used a variety of ways, so I have created a function that encapsulates it in order to open a new URL as a document:

    Public Function doOpenNewWindow(ByVal URL As String, ByVal WindowName As String, Optional ByVal ReplaceHistory As Boolean = False, Optional ByVal Features As String = "") As Object

        'objPrimary is an instance of mshtml.IHTMLDocument2.

        'Create new window using supplied parameters.
        doOpenNewWindow = objPrimary.open(URL, WindowName, Features.GetFeatureText, ReplaceHistory)
    End Function

So a call to this function would look something like this:

        clsTest.doOpenNewWindow("http://www.experts-exchange.com","_self")
        Do While clsTest.readyState <> "complete"
            Application.DoEvents()
        Loop

The loop afterwards is important because this is an asynchronous call, so it would return before the document has been loaded. Once you have used that code to navigate to the page of your choice, you could use something like the following function to access all of the anchors in the document:

    Public Sub DebugPrintAllAnchors()
        'This procedure iterates through all of the anchors in the currently loaded document
        'and demonstrates how to read and write to that element, printing the results to the
        'Immediate window.

        'Variable to hold a mshtml.IHTMLElement object.
        Dim elem As mshtml.IHTMLElement

        'objPrimary is an instance of mshtml.IHTMLDocument2

        'Get a new reference ot the element collection representing all anchors (<A></A>).
        Dim objAnchors As mshtml.IHTMLElementCollection = objPrimary.anchors
        'Iterate through every element in the anchors collection.
        For i As Integer = 0 To objAnchors.length - 1
            'Assign the anchor element to the element placeholder.
            elem = objAnchors.item(i)
            'Print the title, tag name, ID and class name to the Immediate window.
            Debug.Print("Title = " & elem.title & ", TagName = " & elem.tagName & ", ID = " & elem.id & ", Class Name = " & elem.className)

            'Also important: .innerText, .innerHTML, .outerText and .outerHtml
            'outerHTML is especially important, as it returns the entire HTML text for that anchor.

            'If you wanted to assign an attribute to every link, like explicitly commanding every link to open in a new window,
            'you could make the following call:
            elem.setAttribute("target", "_blank")

            'Read it back, make sure it changed:
            Debug.Print(elem.getAttribute("target").ToString)
            'Read the entire tag text for this control:
            Debug.Print(elem.outerHTML)
            Debug.Print("")
        Next
    End Sub

Similarly, if you know the Name of an element that you want to access, you can access that element directly from the .all() element collection. For example, if you wanted to log-in to the Experts Exchange web site programmatically, you could do it like this:

     Public Sub ShowHowToLogIn(ByVal username As String, ByVal password As String)
        'Once the EE site has been loaded (using mshtml.IHTMLDOcument2.open() or the doOpenNewWindow()
        'function that I created above), this procedure will log you in using the
        'username and password that were provided as parameters.

        'objPrimary is an instance of mshtml.IHTMLDocument2

        'Gets reference to username input box.
        Dim elem1 As mshtml.IHTMLElement = objPrimary.all.item("msuLoginName")
        'Assigns the username as the value.
        elem1.setAttribute("value", username)

        'Gets reference to the password input box.
        Dim elem2 As mshtml.IHTMLElement = objPrimary.all.item("msuPassword")
        'Assigns the password as the value.
        elem2.setAttribute("value", password)

        'Gets a reference to the image which serves as the "Log In" button.
        Dim elem3 As mshtml.IHTMLElement = objPrimary.all.item("msuLoginSubmit")
        'Emulates a click of the image.
        elem3.click()
    End Sub

The "msuLoginName," "msuPassword," and "msuLoginSubmit" strings I obtained by simply looking @ the page source for the EE front page.

So, I hope that gives you a leg up on how to use the mshtml DOM interfaces.

 

by: adwooley2Posted on 2006-10-01 at 21:36:42ID: 17641001

BeauT,
That's what I was looking for!  Thank you so much.
I'm currently putting together a recursive class that will do the following:
Take the opening page, find all the "A hrefs" and loop through each calling the creation of mshtml.IHTMLDocument2.  On each loop, it searches for more "A href" and loops through those.  So we go deeper and deeper into the site, then out to the next recursive loop.  
In the process of testing this right now.  If you're interested in the results, let me know.

 

by: BeauTPosted on 2006-10-02 at 00:22:42ID: 17641343

Very interested. I'm still learnign how to use mshtml myself. How are you using the DOM? In other words, to navigate, are you using a web browser control or an InternetExplorer object? Or some other way?

Also, if you have any questions about anything I put up there, definitely ask away. I know it might not be clear, so I will clarify anything you need.

 

by: adwooley2Posted on 2006-10-02 at 00:37:41ID: 17641391

DOM, Shmom...  I'm too new at this to understand your Greek, BeauT.  Ha ha ha ha
OK - Here's what I am doing to crawl over a site.  I'll try to go slow (the only way I CAN go):

1) Get a starting URL

2) Create an HTML object (what I called clsHtmlObj) and pass in the starting URL
Dim newobj As New clsHtmlObj("http://asnet.autoserver.co.jp/asnet20/aa/main/wmaker.asp?w=2")

3) The HTML object is a class that I made, which looks likek this:
=========================================
Imports System.Runtime.InteropServices

<ComVisible(True), ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), _
   InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
   Public Interface IPersistStreamInit
    ' IPersist interface
    Sub GetClassID(ByRef pClassID As Guid)

    <PreserveSig()> Function IsDirty() As Integer
    <PreserveSig()> Function Load(ByVal pstm As UCOMIStream) As Integer
    <PreserveSig()> Function Save(ByVal pstm As UCOMIStream, _
    ByVal ByValByValfClearDirty As Boolean) As Integer
    <PreserveSig()> Function GetSizeMax(<InAttribute(), Out(), _
    MarshalAs(UnmanagedType.U8)> ByRef pcbSize As Long) As Integer
    <PreserveSig()> Function InitNew() As Integer
End Interface
Public Class clsHtmlObj
    Dim mUrl As String = ""
    Dim objMSHTML As New mshtml.HTMLDocument
    Dim doc2 As mshtml.IHTMLDocument2
    Dim doc3 As mshtml.IHTMLDocument3
    Dim ips As IPersistStreamInit
    Dim db As New clsDB

    Public Sub New(ByVal url As String)

        If InStr(url, "http://asnet.autoserver.co.jp/asnet20/aa/main/") > 0 Then  
            If InStr(url, "java") <> 0 Or InStr(url, "#") <> 0 Then
                Exit Sub
            Else
                db.LinkInsert(url)
                frmGrabber.ListBox1.Items.Add(url)
                ips = DirectCast(objMSHTML, IPersistStreamInit)
                ips.InitNew()
                doc2 = objMSHTML.createDocumentFromUrl(url, vbNullString)
                Do Until doc2.readyState = "complete"
                    Application.DoEvents()
                Loop
                doc3 = DirectCast(doc2, mshtml.IHTMLDocument3)
                Me.LoopThroughHrefs(doc3)
            End If
        End If

    End Sub

    Private Sub LoopThroughHrefs(ByVal doc3 As mshtml.IHTMLDocument3)
        Dim col As mshtml.IHTMLElementCollection
        Dim lnk As mshtml.HTMLAnchorElement
        col = doc3.getElementsByTagName("A")

        For Each lnk In col
            If db.LinkSelect(lnk.href) Then
                'GO TO NEXT LINK
            Else
                Dim obj As New clsHtmlObj(lnk.href)
            End If
        Next
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
       

    End Sub
End Class

4) You'll notice that I have a line "Dim db As New clsDB"  
This is for another class that I have written for writing to a SQL Server DB.  If you notice, I try to skip over links that I've already written to the DB.  In the DB, I just have a table called tblLinks and only one nvarchar(200) field to store the URLs that I have traversed.

My clsDB class looks like this:
=========================
Imports System.Data
Imports System.Data.SqlClient
Public Class clsDB
    Dim conString As String = ""
    Dim con As New SqlConnection
    Dim ds As New DataSet
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand

    Public Sub New()
        con.ConnectionString = "Data Source=localhost;Initial Catalog=SBAuction;Integrated Security=True"
    End Sub

    Public Function LinkSelect(ByVal Link As String) As Boolean
        With cmd
            .Connection = con
            .CommandText = "wuspLinkSEL"
            .Parameters.Clear()
            .Parameters.AddWithValue("@Link", Link)
            .CommandType = CommandType.StoredProcedure
        End With
        da.SelectCommand = cmd
        ds.Tables.Clear()
        da.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
   
    Public Sub LinkInsert(ByVal Link As String)
        With cmd
            .Connection = con
            .CommandText = "wuspLinkINS"
            .CommandType = CommandType.StoredProcedure
            .Parameters.Clear()
            .Parameters.AddWithValue("@Link", Link)
            Try
                con.Open()
                cmd.ExecuteNonQuery()
            Catch ex As Exception
            Finally
                con.Close()
            End Try
        End With
  End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        cmd.Dispose()
        con.Dispose()
        ds.Dispose()
        da.Dispose()
    End Sub
End Class


Ummmm.... any of this make sense?  It's not pretty, I'll admit....

If you want me to be a little more descriptive, let me know. Always willing to help a friend.
I'm programming here in Japan, by the way.  Where are you, BeauT?

 

by: BeauTPosted on 2006-10-02 at 05:00:05ID: 17642622

I'm in Vienna, VA, near D.C. It' sfinally getting to be autumn, so I love it.

Anyway, yes, it does make sense to me. Now, are you displaying any of the actual web pages that you call? If you are, what are you using to display them? Also, without a reference to a web browser control or InternetExplorer object, is it still performing correctly?

I ask because the application I am writing appears to only function correctly after I assign a reference to the IE class's Document property (also the case if I decide to use a WebBrowser control instead of the IE object).

        'Create a new instance of Internet Explorer.
        Dim objIE As New SHDocVw.InternetExplorer
        'Navigate to the log-in page; note that the browser is not yet shown...
        objIE.Navigate2("http://www.experts-exchange.com")

        'Wait until page has finished loading.
        'Note that we could also poll until objIE.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_INTERACTIVE
        Do While objIE.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE
            Application.DoEvents()
        Loop

        'Do the same Interop song & dance with the same interfaces declared for the createDocumentFromUrl method.
        'HGDocument is a class I created that implements the 4 usable IHTMLDocument interfaces and includes
        'custom members that help streamline and codify the underlying mshtml objects.
        'In this case, HGDocument.IPersistStreamInit is used since that is where the declaration for the
        'Interop interfaces are located in my project.
        Dim objIPS As HGDocument.IPersistStreamInit = DirectCast(objIE.Document, HGDocument.IPersistStreamInit)
        objIPS.InitNew()

        'clsTest is an instance of a HGDocument object.
        'clsTest.DocumentReference is of type mshtml.IHTMLDocument2.
        clsTest.DocumentReference = objIPS
        '*
        '*  Do some code behind the scenes....
        '*
        'Now show the previously declared instance of IE.
        objIE.Visible = True


Now, it might be that I have to have a reference to the document of an IE instance or a WebBrowser control because some of the processes I have written interact with rendered elements on the page. Now, my biggest stumbling block is this: assigning an instance of IHTMLDocument2 back to IE or a WebBrowser control.

The .Document property is read-only for both objects. The Webbrowser control does have a DocumentStream property that accepts a Stream object, but I'm having trouble getting the IHTMLDocument2 object to convert to a byte array because of it's COM nature. So, either finding a way to convert the IHTMLDocument2 object to a byte array or simply finding a way to use a stream to assign the document is what I am focussing on now. Any thoughts?

 

by: adwooley2Posted on 2006-10-02 at 21:38:00ID: 17649228

BeauT,
Thanks for your reply.  I'll get back to you after I clear a deadline.  

Regards,
Alan

 

by: BeauTPosted on 2006-10-03 at 14:55:42ID: 17655085

Sure thing Alan. :-) Great name, btw. It's the same as my brother's, same spelling and everything.

Also, I've given up on using DocumentStream. Instead, I'm using DocumentText. It works, but, when I load the HTML into the WebBrowser control, I get numerous script errors. Not sure why.
Alternatively (or additionally), the HTML can be assiged directly to an IE instance, using the IPersist interface once again...

        'Create a new instance of Internet Explorer.
        Dim objIE As New SHDocVw.InternetExplorer

        objIE.Navigate2("about:blank")
        Dim objIPS As HGDocument.IPersistStreamInit = DirectCast(objIE.Document, HGDocument.IPersistStreamInit)
        objIPS.InitNew()
        Dim strTemp As String = clsTest.documentElement.outerHTML 'clsTest is an instance of mshtml.IHTMLDocument3
        DirectCast(objIE.Document, mshtml.IHTMLDocument2).write(strTemp)
        Do While objIE.Busy
            Application.DoEvents()
        Loop
        objIE.Visible = True

Also, you must navigate to some page in your IE instance, even if you are not displaying the browser, so that it will create an instance of it's own document before you can write to the document.

One other incidental note: I've experimented with leaving out the .InitNew() method call after creating the IPersistStreamInit interface and, from what I can tell, it appears to initialize the COM object being referred to foruse in .NET. It's just a guess. If anyone wants to add to that.... ;->

 

by: adwooley2Posted on 2006-10-03 at 16:54:11ID: 17655725

Uhhh... looks like you're beyond me. I actually am not displaying the webpage.  My project is as follows:
My company plays middle-man in car web-auctions (we are exporters of cars to Russia, UK, Cyprus, Ireland, Singapore... and on and on). Well, they recently jumped on the web-auction bandwagon, so they need to send out listings of available cars on a daily basis to their customers. I joined the company a couple months ago as they needed somebody to program a cost-analysis web-app for cars going to Pakistan.  The web-auction project came up last week, and they're only giving me a couple more days!!!!   The task is to web-crawl over an auction site, download certain HTML tables into a database, and also download corresponding images of cars and save them to the hard-drive.  What you're doing sounds a little more complicated.  
What exactly is your project all about? I've been learning some weird things... maybe I can help with something? (although I doubt it).

Alan

PS - still scanning here and there about assigning an instance of IHTMLDocument2 back to IE.  ....Can anybody help here?

 

by: BeauTPosted on 2006-10-03 at 17:48:21ID: 17655951

Alan, you might want to consider ditching mshtml altogether if you don't need to render the page or interact with it at all. It would be much faster, and give you much more control, to use the System.net namespace, specifically the WebClient class. With it, you can download the source from any page (without waiting for page rendering, history management or any other browser related tasks) in a synchronous or asynchronous matter and can also download the images much more quickly. Also, by sticking with the System.net namespace, you can avoid having to mess with Interop and unmanaged code.

This article shows how to get the source from a URL in a single shot:
http://www.developer.com/net/vb/article.php/3113371
And this related article shows how to use RegEx (regular expressions) to parse out all links and images:
http://www.developer.com/net/vb/article.php/3113951

Also, if it is not a requirement for you to save the images as actual files, this article shows an easy way to save images to a DB. Since you are saving your data to a DB anyway, maybe this would be more efficient?

http://www.codeguru.com/vb/vb_internet/database/article.php/c7427/

Also, to turn a URL into an Image object (for saving to the DB or a file), you can declare your WebClient object WithEvents and then trap the DownloadDataCompleted method like this:

    Private Sub client_DownloadDataCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs) Handles client.DownloadDataCompleted
        'If action was not cancelled...
        If Not e.Cancelled Then
            'client is the name of the WebClient I have declared.
            'imgLastImage is an object of type Image that I have declared elsewhere.


            'Create stream from returned data.
            Dim objStream As New System.IO.MemoryStream(e.Result)
            'Read data into byte buffer that is then interpreted by the Image object.
            imgLastImage = Image.FromStream(objStream)
        Else
            'Return Nothing.
            imgLastImage = Nothing
        End If
        'Toggle busy status.
        blnBusy = False
    End Sub

This would be used in conjunction with an asynchronous call from the WebClient class.
Unless you have restrictions preventing you from using these classes, I recommend them, based on what I understand of your project. if you need further help, let me know.

And my project is actually progressing ok, I just hate not knowing how to do something, like use the DocumentStream object with mshtml objects. ;->
Although, if you (or anyone) know of any way to completely divorce the IHTMLDocument2 interface from an instance of IE or an instance of a WebBrowser control and then assign it when I choose to do so, that would be great. As it is, in order to interact with elements of the DOM, I have to create a document in either a WebBrowser control or an instance of IE and then assign it to my custom document handler. It's not a big deal, but I hate having to create additional objects in memory that may never be directly needed.

 

by: adwooley2Posted on 2006-10-03 at 18:01:10ID: 17655992

This is very good to know! I appreciate it.  Just wish that I could reciprocate.
I'll look into this during the next version.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...