I want to access Internet Explorer using VBA.
Dim ie As InternetExplorer
Dim i As IHTMLDocument
Dim d As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "www.google.com"
Set d = ie.Document
I want to use intellisense that is why instead of going for ie.document.get I am using this method.
Getting error like this:
The issue isn't related with intellisense. All you need is to wait until navigating is completed and IE is ready before accessing document:
Sub Test()
Dim ie As InternetExplorer
Dim i As IHTMLDocument
Dim d As HTMLDocument
Set ie = New InternetExplorer
With ie
.Visible = True
.Navigate "https://www.google.com"
Do While .ReadyState < READYSTATE_COMPLETE Or .Busy: DoEvents: Loop
Set d = .Document
End With
End Sub
Also take a look at this answer.
Related
I am into Project Management with no knowledge of IT / Coding.
I am trying to do web automation.
It will have to click on certain links, submit a form and then hit save.
However I am not able to click on the first link as well.
this is my code as of now.
Sub CommandButton1_Click()
Dim ie As Object
Dim html As HTMLDocument
Dim form As Variant, button As Variant
Sheet1.Range("B6").Value = Application.UserName
'Open Internet Explorer
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://www.link.com/"
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
html.getElementsByClassID("p2205").Click
End Sub
Below is the code.
I want to click on Company Name.
Website Code
I have tried the web and YouTube videos as well but I guess I don't know the language or the logic behind coding. Hence I am not able to get through.
Add reference to Microsoft Internet Controls then try
Option Explicit
Sub CommandButton1_Click()
Dim ie As Object, html As HTMLDocument, e As HTMLHtmlElement
Dim URL As String
URL = "https://www.link.com/"
Set ie = New InternetExplorerMedium
ie.navigate URL
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
For Each e In html.getElementsByClassName("dsh_sta_301")
If InStr(e.onclick, "projectSelection") > 0 Then
e.Click
End If
Next
End Sub
I'm trying to get the innertext of a label but i'm getting an error. Through the console i'm succesfully getting the inner text with this script :
document.getElementsByClassName("item alt")[0].childNodes[2].childNodes[0].innerText
Element i'm trying to get :
<tr class="item alt" data-id="1376936"><td class="toolbar left"><span class="ui-icon ui-icon-triangle-1-e"></span></td><td class="time">14:00</td><td class="status"><span class="status-1 rc">FT</span>
My VBA script :
Sub WebScraping()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.whoscored.com/Regions/74/Tournaments/22/Seasons/7814/Stages/17593/Fixtures/France-Ligue-1-2019-2020"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to Whoscored ..."
DoEvents
Loop
Set doc = ie.document
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to Whoscored ..."
DoEvents
Loop
Set a = doc.getElementsByClassName("item alt")(0).ChildNodes(2).ChildNodes(0).innerText
MsgBox (a)
End Sub
Set a = doc.getElementsByClassName("item alt")(0).ChildNodes(2).ChildNodes(0).innerText
Try to use the getElementsByClasssName method to find the child node, please modify above code as below:
Dim a As String
a = doc.getElementsByClassName("item alt")(0).getElementsByClassName("status")(0).getElementsByClassName("status-1")(0).innerText
MsgBox (a)
The first line in every module should be Option Explicit.
I'm not sure what you want at all. But to show the wanted element use this:
Sub WebScraping()
Dim ie As InternetExplorer
Dim doc As HTMLDocument
Dim a As Object
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.whoscored.com/Regions/74/Tournaments/22/Seasons/7814/Stages/17593/Fixtures/France-Ligue-1-2019-2020"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to Whoscored ..."
DoEvents
Loop
Application.StatusBar = False
Set doc = ie.document
Set a = doc.getElementsByClassName("item alt")(0).getElementsByClassName("status")(0)
MsgBox a.innerText
End Sub
I'd like to bring the IE.Document of one window into the IE.document of another window. Can this be done?
I can't see the sense behind your question. But you can do that in this way:
Sub DocFromIEtoIE()
Dim ieOne As Object
Dim ieTwo As Object
Dim nodeHTML As Object
Dim htmlDoc As String
Dim url As String
url = "https://stackoverflow.com/"
Set ieOne = CreateObject("internetexplorer.application")
ieOne.Visible = True
ieOne.navigate url
Do Until ieOne.ReadyState = 4: DoEvents: Loop
Set nodeHTML = ieOne.document.getElementsByTagName("html")(0)
If Not nodeHTML Is Nothing Then
htmlDoc = nodeHTML.outerHTML
Set ieTwo = CreateObject("internetexplorer.application")
ieTwo.Visible = True
ieTwo.navigate "about:blank"
Do Until ieTwo.ReadyState = 4: DoEvents: Loop
ieTwo.document.Write (htmlDoc)
End If
End Sub
I'm new to using VBA. I'm trying to wrap my head around using it correctly. I have this code and it seems to work perfectly the 1st time around but then I can't get it to work again. I'm trying to get to the match summary page for basketball games played. If I could get some assistance it would be much appreciated
Sub Test()
Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim i As Integer
URL = "http://www.flashscore.com/basketball/"
Set IE = New InternetExplorer
With IE
.Navigate URL
.Visible = True
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set HTMLdoc = .Document
End With
Set TDelements = HTMLdoc.getElementsByTagName("td")
For Each TDelement In TDelements
If TDelement.Title = "Click for match detail!" Then
TDelement.Click
End If
Next
End Sub
try using this
Dim IE as Object
Set IE = CreateObject("InternetExplorer.Application")
--corrected
Your code actually looks fine with one small tweak- make sure to add:
IE.Quit
Set IE = Nothing
End Sub
Otherwise multiple IE instances would be hiding in the background- others have suggested using CreateObject("InternetExplorer.Application") but I much prefer using the methodology you used so you have a true InternetExplorer object that you can see its methods & properties if necessary..
I ran it and didn't get an errors, but also didn't get any TDs to meet the If clause- this could just be due to the timing of the website and its data, element.title is not an attribute I have seen used very often so perhaps this is the source of your issues and you should revisit what attribute you are checking to meet your if clause.
Hope this helps,
ThesilkCode
Through Excel and VBA, i am opening a page.
I want to click on the button 'Expand' through vba but I am getting an error.
I tried using both VBA commands listed below
Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")(0).Click
Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all").Click
The error that I get is
Run time error '438': Object doesn't support this property or method.
Assuming that "Doc" is your reference to IE try this:
Set ElementCol = Doc.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")
For Each btnInput In ElementCol
btnInput.Click
Next btnInput
Working Example:
Private Sub IE_Expand()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'IE.Visible = False
IE.Navigate "https://dhli.dhl.com/dhli-client/shipmentair;jsessionid=q3tzTzyLcL7JkxkNQ4nv7Jtrpzk1glylCyJ7vJzT27h2xBG5zXSm!599496067?0&shipmentId=151218573&accountGroup"
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.Visible = True
Set ElementCol = IE.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")
ElementCol.Item(0).Click
' Clean up
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
Application.StatusBar = ""
End Sub