PDA

View Full Version : Difference between .responsetext and .document



xlcior
05-09-2012, 01:12 PM
I'm trying to understand the difference between .responsetext and .document. Can someone provide an explanation?

Here are two pieces of code. The first fails and the second is successful, but I do not understand why. I feel like both should return the contents of a website, but they do not. It's strange because I have other methods that successfully use IE.document.all("text").Value to return specific information from part of a website. I guess it can't return all of the website content?

First code:
Dim IE As New InternetExplorer
IE.Visible = True
IE.navigate "website"

Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE

Dim xmlHttp As Object
Dim Doc As String


xmlHttp = IE.document.Value
Doc = xmlHttp.responseText


Second code:
strURL = "website"
Set xmlHttp = CreateObject("msxml2.xmlhttp")
With xmlHttp
.Open "get", strURL, False
.send
pageText = .responseText
End With

Kenneth Hobs
05-09-2012, 04:21 PM
Document is an object. Remove .Value. Of course msdn documents what responsetext means. http://msdn.microsoft.com/en-us/library/ie/ms534369%28v=vs.85%29.aspx

xlcior
05-09-2012, 04:33 PM
Thanks for the reply Kenneth. In that case, I would assume the following returns a string, but it fails. What am I doing wrong? How should I modify the statement below?

Doc = IE.document.responseText

Kenneth Hobs
05-09-2012, 06:34 PM
I don't see responseText a member of the Document object. Intellisense will show you if it is. You are probably better off using each as needed.

For an example of early binding for WinHttpRequest object see: http://vbaexpress.com/forum/showthread.php?t=25616

Here is an example using the internetexplorer object for my login to Yahoo. I only include it here to show the references.

Sub Test_LoginYahoo()
LoginYahoo ThisWorkbook.Worksheets("Main").Range("A1").Value2, _
ThisWorkbook.Worksheets("Main").Range("A2").Value2
End Sub

' Add references in Tools > References for:
' Microsoft HTML Object Library
' Microsoft Forms 2.0 Object Library
' Microsoft Internet Controls
Sub LoginYahoo(username As String, password As String)
Const strURL_c As String = "http://mail.yahoo.com"
Dim objIE As SHDocVw.InternetExplorer
Dim ieDoc As MSHTML.HTMLDocument
Dim tbxPwdFld As MSHTML.HTMLInputElement
Dim tbxUsrFld As MSHTML.HTMLInputElement
Dim btnSubmit As MSHTML.HTMLInputElement

Excel.Application.Cursor = xlWait
If InStr(username, "@") = 0 Then username = username & "@yahoo.com"

On Error GoTo Err_Hnd

'Create Internet Explorer Object
Set objIE = New SHDocVw.InternetExplorer
'Navigate the URL
objIE.Navigate strURL_c
objIE.Visible = False
'Wait for page to load
Do Until objIE.ReadyState = READYSTATE_COMPLETE: Loop
'Do While objIE.Busy Or objIE.ReadyState <> READYSTATE_COMPLETE
' DoEvents
'Loop
'Set document object
Set ieDoc = objIE.document
ieDoc.getElementsByName("passwd").Item(0).Value = password
ieDoc.getElementsByName("username").Item(0).Value = username
ieDoc.forms("Login_form").Submit

Err_Hnd: '(Fail gracefully)
objIE.Visible = True
On Error GoTo 0
Excel.Application.Cursor = xlDefault
End Sub