Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Internet Explorer Object 2

Status
Not open for further replies.

LotusE

Programmer
Nov 22, 2002
25
BE
I would like to be able to check the contents of certain webpages for certain words.

Right now, when you click with your right mouse button on a web page (in internet explorer), you get a little menu and one of the menu-items is 'View Source', so you can view the source.

I would like to be able to automate this somehow in Foxpro and look for some specific words in the source of certain webpages.

Is this possible? And if so, how?

Thanks in advance!

Cheers

LotusE
 
Check out FAQ1251-5156 and FAQ1251-5279.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
LotusE,

One way of doing that is to use the Internet Transfer control, which is one of the ActiveX controls that come with VFP. You can use it to retrieve an HTML page from a web server. An HTML page is just a text file (this is what you see when you select View Source in IE), so you can use all of VFP's string handling and searching functions on it.

If you need any help with using the Internet Transfer control, just ask. I won't give any more details just now because I don't know how much you already know about it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Code:
* Review the method "GetHtmlContents()" in the class definition "MyForm".

* For the IE object model see:
* [URL unfurl="true"]http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/objects/internetexplorer.asp[/URL]


local oForm
oForm = createobject("MyForm")
oForm.show()
read events

define class MyForm as form
  docreate = .t.
  autocenter = .t.
  datasession = 2
  width = 800
  height = 500
  caption = "View HTML page's source"
  cURL = ""
  cHTMLSource = ""

  add object lblURL as label with ;
    top = 5, ;
    left = 5, ;
    caption = "Enter a valid URL", ;
    autosize = .t.

  add object txtURL as textbox with ;
    left = 5, ;
    top = this.lblURL.top + this.lblURL.height , ;
    width = 500, ;
    height = 24, ;
    fontname = "courier new", ;
    controlsource = "thisform.cURL"

  add object cmdGetHTML as commandbutton with ;
    top = this.txtURL.top, ;
    width = 100, ;
    height = 24, ;
    left = this.txtURL.left + this.txtURL.width + 4, ;
    caption = "Get HTML"

  add object lblHTML as label with ;
    left = 5, ;
    top = this.txtURL.top + this.txtURL.height + 5, ;
    caption = "Retrieved HTML Source", ;
    autosize = .t.

  add object edtHTML as editbox with ;
    left = 5, ;
    top = this.lblHTML.top + this.lblHTML.height, ;
    width = this.width - 10, ;
    height = this.height - this.lblHtml.top + this.lblHTML.height - 40, ;
    fontname = "courier new", ;
    controlsource = "thisform.cHTMLSource"


  procedure load
    set cent on
    set safe off
    set escape on
  endproc

  procedure destroy
    clear events
  endproc

  procedure cmdGetHTML.click
    thisform.GetHTMLContents()
  endproc

  procedure GetHtmlContents
    this.cHTMLSource = ""
    this.refresh()

    local oIE, cDocContents, oWaitForm
    oWaitForm = createobject("frmWait")
    oWaitForm.SetMessage("Please Wait...")
    oWaitForm.show()

    oIE = createobject('internetexplorer.application')
    oIE.Navigate(this.cURL)

    * Wait until IE navigates to the site
    do while oIE.busy
    enddo

    * Wait until IE finishes parsing the document
    do while oIE.document.readystate <> "complete"
    * (On some sites it makes a difference and
    *  the document will not be parsed yet even
    *  though the busy flag of IE is true)
    enddo

    * Get the document contents
    cDocContents = ""

    if oIe.document.all(0).tagname == "!"
      * If the first element is a DTD
      cDocContents = oIe.document.all(0).text + chr(13)+chr(10)

      * The second element will be of type "html"
      cDocContents = cDocContents + oIe.document.all(1).outerhtml
    else
      * The first element is of type "html"
      cDocContents = oIe.document.all(0).outerhtml
    endif

    oIE.quit

    this.cHTMLSource = cDocContents

    this.refresh()

    oWaitForm.release()

    this.edtHTML.setfocus()

  endproc
enddefine

* Wait window
define class frmWait as form
  docreate = .t.
  autocenter = .t.
  borderstyle = 0
  titlebar = 0
  width = 200
  height = 50
  alwaysontop = .t.

  cMessage = ""

  add object txtMessage as textbox with ;
    top = 2,;
    left = 2, ;
    alignment = 2, ;
    width = this.width -4, ;
    height = this.height - 4, ;
    forecolor = rgb(255,255,255), ;
    backcolor = rgb(0,0,0), ;
    fontsize = 14, ;
    controlsource = "thisform.cMessage"

  procedure SetMessage
    lparam cMessage
    this.cMessage = cMessage
    this.refresh()
  endproc

enddefine
 
Thanks guys, this was what I was looking for!!! Now I can continue developing my software!!

Thanks alot!!!


Cheers!!!

LotusE
 
darrellblackhawk,

I like the time you took to put together a little interface for this, looks a bit like a solutions sample. Have a star from me.

boyd.gif

 
I made a slight modification of the example code I posted yesterday.

I noticed that sometimes the document object creation gets
delayed between testing IE's busy flag and testing for the
document readystate - which will cause an error.

Make the changes hightlighted in [red]Red[/red] to the
GetHtmlContents() method of the previously posted example.

Darrell


Code:
    * Wait until IE navigates to the site
    do while oIE.busy
    enddo
[red]
    * In case the document object creation gets delayed
    do while type("oIE.document")=="U"
    enddo
[/red]
    * Wait until IE finishes parsing the document
    do while oIE.document.readystate <> "complete"
      * (On some sites it makes a difference and
      *  the document will not be parsed yet even
      *  though the busy flag of IE is true)
    enddo
 
... and a star from me too!!!!

Cheers

LotusE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top