Perform operations in different IE tabs - excel

I would like to search for city in one IE tab then open my generated link in another tab. If I run these operations separately everything work however I am not able to make it work all together. What is possibly wrong?
Debugger points to .Document.getElementsByName("q")(0).Value = "Dublin" saying
Run-time error '-2147467259 (80004005)': Method 'Document' of object
'IWebBrowser 2' failed
Here is my code:
Option Explicit
Sub GetFlightRates()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://maps.google.com"
.Document.getElementsByName("q")(0).Value = "Dublin"
.Document.getElementsByClassName("searchbox-searchbutton")(0).Click
.Navigate "https://www.google.co.in/flights/", CLng(2048) '2nd
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
'appIE.Quit
Set appIE = Nothing
End Sub

You need a page load wait before attempting to access .document
Option Explicit
Sub OpenTabs()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Visible = True
.navigate "https://maps.google.com"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementsByName("q")(0).Value = "Dublin"
.document.getElementsByClassName("searchbox-searchbutton")(0).Click
.navigate "https://www.google.co.in/flights/", CLng(2048) '2nd
While .Busy Or .readyState < 4: DoEvents: Wend
End With
Stop
'appIE.Quit
Set appIE = Nothing
End Sub

Related

Extract hyperlink using CSS selector query

I would like to extract the hyperlink from a webpage by using queryselector all, but there are no results coming out.
Below is my code.
Sub ScrapLink()
Application.ScreenUpdating = False
Dim IE As New InternetExplorer, html As HTMLDocument
Dim x As Long
Application.ScreenUpdating = False
With IE
IE.Visible = True
IE.Navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"
While .Busy Or .ReadyState < 4: DoEvents: Wend
Application.Wait Now + TimeSerial(0, 0, 1)
DoEvents
With .Document.getElementById("bm_ann_detail_iframe").contentDocument
Dim links As Object, i As Long
Set links = .Document.querySelectorAll("p.att_download_pdf[href^='/FileAccess/apbursaweb/']")
For i = 1 To links.Length
With ThisWorkbook.Worksheets("Sheet1")
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = links.Item(i - 1)
End With
Next i
.Quit
End With
End With
End Sub
You could just avoid the initial page and use the URL direct from the frame. This would be my preference unless you don't know, for some reason, this URL.
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer, nodeList As Object, i As Long
With IE
.Visible = True
.navigate2 "http://disclosure.bursamalaysia.com/FileAccess/viewHtml?e=2906127"
While .Busy Or .readyState < 4: DoEvents: Wend
Set nodeList = .document.querySelectorAll(".att_download_pdf [href^='/FileAccess/apbursaweb/download']")
For i = 0 To nodeList.Length - 1
Debug.Print nodeList.item(i).href
Next
.Quit
End With
End Sub
Or you can jump right on over to the iframe src after page load:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer, nodeList As Object, i As Long
With IE
.Visible = True
.Navigate2 "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"
While .Busy Or .readyState < 4: DoEvents: Wend
.Navigate2 .document.querySelector("iframe").src
While .Busy Or .readyState < 4: DoEvents: Wend
Set nodeList = .document.querySelectorAll(".att_download_pdf [href^='/FileAccess/apbursaweb/download']")
For i = 0 To nodeList.Length - 1
Debug.Print nodeList.item(i).href
Next
.Quit
End With
End Sub
Try the following. It should fetch you the links you wish to grab:
Sub ScrapLink()
Dim IE As New InternetExplorer, Html As HTMLDocument
Dim frame As Object, i As Long
With IE
.Visible = True
.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
Application.Wait Now + TimeValue("00:00:03") 'This delay may vary in your case
Set frame = Html.getElementById("bm_ann_detail_iframe").contentWindow.document
With frame.querySelectorAll("p.att_download_pdf a")
For i = 0 To .Length - 1
Cells(i + 1, 1) = .item(i).getAttribute("href")
Next i
End With
End Sub
If you wish to kick out the delay then try changing the portion below with the above one:
Do: Set frame = Html.getElementById("bm_ann_detail_iframe"): DoEvents: Loop While frame Is Nothing
With frame.contentWindow.document.querySelectorAll("p.att_download_pdf a")
For i = 0 To .Length - 1
Cells(i + 1, 1) = .item(i).getAttribute("href")
Next i
End With

VBA Run-time error '-2147417848 (80010108)'

I am trying to connect to a webpage and enter a value but it's giving me the runtime error as follows:
Run-time error '-2147417848 (80010108)'.
Here is my code:
Sub OpenMyURL()
Dim eRow As Long
Dim ele As Object
Set objIE = CreateObject("InternetExplorer.Application")
myjobtype = InputBox("Enter offer name")
With objIE
.Visible = True
.navigate "visualreports.aexp.com/#/views/AmexOffersPersistenceReport/…; 'getting runtime error here "
Do While .Busy Or .readyState <> 4
DoEvents
Loop
Set what = objIE.document.getElementsByName("omniboxTextBox")
what.Item(0).Value = myjobtype
.document.getElementById("JobsButton").Click
Do While .Busy Or .readyState <> 4
DoEvents
Loop
Set objIE = Nothing
End Sub

IE.Document.getElementsByName("name").Value = "Value" not working

When I try and do this, it throws a number of errors at me depending on other bits of my code, such as the method is not associated with that object or an unspecified error.
I am trying to go down a list of Excel cells with strings and use a web form to search for those strings.
My code currently looks like this:
Sub YSF_automation()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "URL"
While IE.Busy
DoEvents
Wend
IE.Document.getElementsByName("ElementName")(0).Value = "test"
End Sub
Try the below
Option Explicit
Dim IE as Object
Sub YSF_automation()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "URL"
While IE.Busy
DoEvents
Wend
set document = IE.document
with document
.getElementsByName("ElementName")(0).value="test"
end with
End Sub

VBA to select list in IE

I've been trying to use VBA to open an IE window and select from one of the dropdown list showing bwin but failed. Any advice?
Private Sub OKButton_Click()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.Navigate "http://www.yahoo.com/"
End With
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
Set AvailableLinks = oIE.document.getelementbyid("list-listing").getelementsbytagname("a")
For Each cLink In AvailableLinks
If cLink.innerhtml = "????" Then
cLink.Click
End If
Next cLink
End Sub

Runtime Error 429: ActiveX component can't create object” in CreateObject("InternetExplorer.Application")

Hello am getting this error while executing the following code
Sub TableExample()
Dim IE As Object
Dim doc As Object
Dim strURL As String
strURL = "http://www.idealo.de/preisvergleich/OffersOfProduct/143513.html"
Set IE = CreateObject("InternetExplorer.Application")
With IE
'.Visible = True
.navigate strURL
Do Until .readyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
Set doc = IE.document
GetAllTables doc
.Quit
End With
End Sub
The error is getting in this line
Set IE = CreateObject("InternetExplorer.Application")
If I change it to
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
I am using 64bit Windows 7 and office 2010 version
then what should be the code can anyone guide me on this please?

Resources