ADODB : Not able to save file after dowload - vba

I am using the code below to download PDF files from a website and then I want to save them in local drive.
I am trying to save files using oStream.SaveToFile method but files are being downloaded in corrupted format error. Or when I'm using oStream.Save method, files are not being downloaded at all. Please help.
Sub login()
Dim objIE As InternetExplorer
Dim uid As String
Dim pwd As String
Dim rng As Range
Dim sh As Worksheet
Dim objCollection As Object
Dim buttonCollection As Object
Dim ieElement As Object
Dim ieButton As Object
Dim WinHttpReq As Object
Dim oStream As Object
Dim MyURL As String
Application.ScreenUpdating = False
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
Set sh = Sheets("Indeed Resume Download")
Set rng = sh.Range("A2")
If Trim(rng.Value) = "" Or Trim(rng.Offset(0, 1).Value) = "" Then
MsgBox "User ID And Password are mandatory."
Exit Sub
End If
On Error Resume Next
objIE.Quit
Set objIE = Nothing
On Error GoTo 0
uid = rng.Value
pwd = rng.Offset(0, 1).Value
Dim j As Long
Set objIE = New InternetExplorer 'Initialize internet object
objIE.Navigate "https://secure.indeed.com/account/login?service=my&hl=en_IN&co=IN&continue=https%3A%2F%2Fwww.indeed.co.in%2F"
objIE.Visible = True
With objIE
Do While .Busy: Loop
Do While .ReadyState <> READYSTATE_COMPLETE: Loop
Do While .Busy: Loop
End With
objIE.Document.all.signin_email.Value = uid
objIE.Document.all.signin_password.Value = pwd
Set ieElement = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")(0)
ieElement.Click
For j = 2 To sh.Cells(Rows.Count, 27).End(xlUp).Row
Set objIE = New InternetExplorer
objIE.Visible = True
Dim Sender As String
MyURL = sh.Range("XDA" & j).Value
Sender = sh.Range("XDB" & j).Value
objIE.Navigate MyURL
Do While objIE.Busy = True
DoEvents
Loop
Do While objIE.Busy: Loop
Do While objIE.ReadyState <> READYSTATE_COMPLETE: Loop
Do While objIE.Busy: Loop
On Error Resume Next
Set ieButton = objIE.Document.getElementsByClassName("button download_button")(0)
ieButton.Click
WinHttpReq.Open "GET", MyURL, False
WinHttpReq.Send
MyURL = WinHttpReq.responseBody
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.Save ("D:\Downloaded Indeed Resume - Store")
'oStream.SaveToFile ("D:\Downloaded Indeed Resume - Store\" & Sender & ".pdf"), adSaveCreateOverWrite
oStream.Close
Next j
Set objIE = Nothing
End Sub

Related

'Busy' method of 'IUWebBrowser2' object failed

When I launch the site via my code, there is an error of type "method document of object iwebbrowser2 failed " at the level of my variable "oDoc"
Private Function CreerNavigateur(ByVal mails As String)
Dim IE As Object
Dim oDoc As Object
Dim Htable, maTable As Object
Dim text As String
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate "https://csrtool-ssl.sso.infra.ftgroup/csrtool_web/Bricks/pg/osuit/pages/identity/IdentityAccountAndUsers?type=emailAlias&value=" & mails & "&tab_main=AccountInfo"
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set oDoc = IE.Document
Set Htable = oDoc.getElementsByTagName("div")(1)
' MsgBox Htable.innerhtml
Set maTable = Htable.getElementsByTagName("span")
'MsgBox maTable(0).href
'myData = maTable(0).innertext
'MsgBox (myData)
IE.Quit
'On libère les variables
Set IE = Nothing
Set oDoc = Nothing
End Function
thank you for helping me to see my mistake
formalizing my answer try maximized ie window and updated code will be
Private Function CreerNavigateur(ByVal mails As String)
Dim ie As SHDocVw.InternetExplorer
Dim oDoc As Object
Dim Htable, maTable As Object
Dim text As String
Set ie = New SHDocVw.InternetExplorer
ie.Visible = False
ie.navigate "https://csrtool-ssl.sso.infra.ftgroup/csrtool_web/Bricks/pg/osuit/pages/identity/IdentityAccountAndUsers?type=emailAlias&value=" & mails & "&tab_main=AccountInfo"
While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set oDoc = ie.Document
Set Htable = oDoc.getElementsByTagName("div")(1)
'MsgBox Htable.innerhtml
Set maTable = Htable.getElementsByTagName("span")
'MsgBox maTable(0).href
'myData = maTable(0).innertext
'MsgBox (myData)
ie.Quit
'On libère les variables
Set ie = Nothing
Set oDoc = Nothing
End Function

Downloaded File: Not able to save to local drive

I've a VBA script which download files from a website but when the PDF file is being downloaded and when I tried to save it to my local drive of the system I'm getting an error called run time error '3004': Write to file failed. My code is as following:
Sub login()
Dim objIE As InternetExplorer
Dim uid As String
Dim pwd As String
Dim rng As Range
Dim sh As Worksheet
Dim objCollection As Object
Dim buttonCollection As Object
Dim ieElement As Object
Dim ieButton As Object
Dim WinHttpReq As Object
Dim oStream As Object
Dim MyURL As String
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
Set sh = Sheets("Indeed Resume Download")
Set rng = sh.Range("A2")
uid = rng.Value
pwd = rng.Offset(0, 1).Value
Dim j As Long
Set objIE = New InternetExplorer 'Initialize internet object
objIE.Navigate "https://secure.indeed.com/account/login?service=my&hl=en_IN&co=IN&continue=https%3A%2F%2Fwww.indeed.co.in%2F"
objIE.Visible = True
objIE.Document.all.signin_email.Value = uid
objIE.Document.all.signin_password.Value = pwd
Set ieElement = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")(0)
ieElement.Click
For j = 2 To sh.Cells(Rows.Count, 27).End(xlUp).Row
Set objIE = New InternetExplorer
objIE.Visible = True
MyURL = sh.Range("XDA" & j).Value
objIE.Navigate MyURL
Do While objIE.Busy = True
DoEvents
Loop
Set ieButton = objIE.Document.getElementsByClassName("button download_button"(0)
ieButton.Click
WinHttpReq.Open "GET", MyURL, False
WinHttpReq.Send
MyURL = WinHttpReq.responseBody
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile ("D:\Downloaded Indeed Resume - Store")
oStream.Close
Next j
Set objIE = Nothing
End Sub

Not able to login to my indeed account using VBA

On line:
Set ieElement = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")
ieElement.Click of the below code throws an error called "Object does not support this property or method".
Sub login()
Dim objIE As InternetExplorer
Dim uid As String
Dim pwd As String
Dim rng As Range
Dim sh As Worksheet
Dim ieElement As Object
Set sh = Sheets("Indeed Resume Download")
Set rng = sh.Range("A2")
On Error Resume Next
objIE.Quit
Set objIE = Nothing
On Error GoTo 0
uid = rng.Value
pwd = rng.Offset(0, 1).Value
Dim j As Long
Set objIE = New InternetExplorer 'Initialize internet object
objIE.Navigate "https://secure.indeed.com/account/loginservice=my&hl=en_IN&co=IN&continue=https%3A%2F%2Fwww.indeed.co.in%2F"
objIE.Visible = True
objIE.Document.all.signin_email.Value = UserID
objIE.Document.all.signin_password.Value = Password
Set ieElement = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")
ieElement.Click
For j = 2 To sh.Cells(Rows.Count, 27).End(xlUp).Row
Set objIE = New InternetExplorer
With objIE
.Navigate sh.Range("CA" & j).Value
Do While .Busy = True
DoEvents
Loop
Do While .Busy: Loop
Do While .ReadyState <> READYSTATE_COMPLETE: Loop
Do While .Busy: Loop
End With
Do While objIE.Busy = True
DoEvents
Loop
Next j
Set objIE = Nothing
End Sub
When you use getElementsByClassName, you have to determine which class name you are wanting, as there can be many. If your class name is unique (which in this case it appears it could be), you can simply add a (0) to the end. If you want to search through all the class names you could use a For Each...Next statement.
Notice the different between these:
getElementByID() and getElementsByClassName()? Elements is plural when used with ClassName, so you need to designate which element of that class you want.
Try replacing your line with this:
Set ieElement = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")(0)
The method you are currently using would work along these lines:
Dim ieElements, ieElemBtn, ieElement
Set ieElements = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")
For Each ieElement in ieElements
If ieElement ..... Then
Set ieElemBtn = ieElement
Exit For
End If
Next ieElement
ieElemBtn.Click

Object doesn't support this property or method while getting the first result of bing search

I am trying to get the link first result of bing search:
Sample url: https://www.bing.com/search?q=Lancaster University Statistics and Operational Research with Industrial Applications MRes
but I am getting run time error 438 on this line:
Set objLink = objResultDiv.getelementsbytagname("a")(0) 'Error here
Sub getURL()
Dim objIE As Object
Dim i As Long, fI As Long
Dim fURL
Dim objResultDiv
Dim objLink
Application.ScreenUpdating = True
fI = INP.Range("D" & Rows.Count).End(xlUp).Row
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
.Silent = True
For i = 1 To fI
.Navigate INP.Range("D" & i).Value
Do While .Busy Or .readyState <> 4
DoEvents
Loop
fURL = Empty
Set objResultDiv = objIE.document.getelementsbyclassname("b_algo")
Set objLink = objResultDiv.getelementsbytagname("a")(0) 'Error here
INP.Range("E" & i) = objLink.href
Next i
End With
objIE.Quit
Set objIE = Nothing
End Sub
getElementsByClassname returns a collection of elements. So you'll need to specify which one to return. For example, to return the first element (index starts at 0)...
Set objResultDiv = objIE.document.getElementsByClassname("b_algo")(0)
Hope this helps!

VBA HTML Scraping - '.innertext' from complex table

All,
I've created the following Module to scrape a single value (1m % change in London house prices) from the below address:
https://www.hometrack.com/uk/insight/uk-cities-house-price-index/
The specific value is nested within the following code:
The below VBA code is my attempt at scraping. I, perhaps wrongly, feel that I am very close to capturing the value - but the code will not work.
Does anyone know where I am going wrong here? It doesn't show an error message but also doesn't output any values.
Sub HousePriceData()
Dim wb As Workbook
Dim ws As Worksheet
Dim TxtRng As Range
Dim ie As Object
Dim V As Variant
Dim myValue As Variant
Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
ie.NAVIGATE "https://www.hometrack.com/uk/insight/uk-cities-house-price-index/"
ie.Visible = False
While ie.ReadyState <> 4
DoEvents
Wend
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Input")
Set TxtRng = ws.Range("C15")
Set myValue = ie.document.getElementById("cities-index-table").getElementsByTagName("tr")(7).g‌​etElementsByTagName("td")(5)
TxtRng = myValue.innerText
End Sub
Try to use XHR and primitive parsing instead of awkward IE:
Sub Test()
Dim strUrl As String
Dim strTmp As String
Dim arrTmp As Variant
strUrl = "https://www.hometrack.com/uk/insight/uk-cities-house-price-index/"
With CreateObject("MSXML2.XMLHttp")
.Open "GET", strUrl, False
.Send ""
strTmp = .ResponseText
End With
arrTmp = Split(strTmp, ">London</a></td>", 2)
strTmp = arrTmp(1)
arrTmp = Split(strTmp, "<td>", 7)
strTmp = arrTmp(6)
arrTmp = Split(strTmp, "</td>", 2)
strTmp = arrTmp(0)
ThisWorkbook.Sheets("Input").Range("C15").Value = strTmp
End Sub
try use this
Dim Engmt As String
Engmt = "ERRORHERE"
On Error Resume Next
Engmt = Trim(ie.document.getElementById("cities-index- table").getElementsByTagName("tr")(12).g‌​etElementsByTagName("td")(4).innerText)
On Error GoTo 0
If Engmt = "ERRORHERE" Then
TxtRng.Value = "ERROR"
Else
TxtRng.Value = Engmt
End If