Can I write an InternetExplorer.document to another InternetExplorer window? - excel

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

Related

VBA DOM getElementsBy can't get childnodes

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 am not able to use the below code to login to a website using Internet Explorer. It says runtime error 424 - Object required

The line where I'm getting the error is :
oBrowser.Document.getElementById("LOGUSER").Value = "yyyyyyyy"
Below is the full code:
Sub website()
On Error GoTo 0
Dim sURL As String
Dim oBrowser As InternetExplorerMedium
Dim HTMLDoc As HTMLDocument
Dim oHTML_Element As IHTMLElement
sURL = "https://xyxyxyxyxyyyx"
Set oBrowser = New InternetExplorerMedium
oBrowser.Silent = True
oBrowser.navigate sURL
Do Until oBrowser.readyState = 4
DoEvents
Loop
oBrowser.Visible = True
oBrowser.Document.getElementById("LOGUSER").Value = "yyyyyyyyy"
oBrowser.Document.getElementById("LOGPASS").Value = "sgsgrger"
oBrowser.Document.getElementById("Login").Click
Do Until Not oBrowser.Busy And oBrowser.readyState = 4
DoEvents
Loop
End Sub
Make sure that HTML element with an ID LOGUSER is available in your web page.
If your code is not able to find that element than it will generate this error.
Try to check that spelling is correct and it was written exactly as in your web page.
I try to test your code and I try to modify it little bit and looks like it is working fine on my side.
Sub website()
On Error GoTo 0
Dim sURL As String
Dim oBrowser As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim oHTML_Element As IHTMLElement
sURL = "C:\Users\Administrator\Desktop\75.html"
Set oBrowser = CreateObject("InternetExplorer.Application")
oBrowser.navigate sURL
Do Until oBrowser.readyState = 4
DoEvents
Loop
oBrowser.Visible = True
oBrowser.Document.getElementById("LOGUSER").Value = "abcd"
oBrowser.Document.getElementById("LOGPASS").Value = "xyz"
oBrowser.Document.getElementById("Login").Click
End Sub
Output:
You can try to check on your side and let us know if issue persist, We will try to provide further suggestions.

Upload a file into a web page using VBA

I am trying to upload a file into a web page. I followed the following steps:
Open the webpage
looking for "input" tagname
once tagname is found, open the browser
Enter the file path name in the file location.
Exit loop.
Problem:
At step-3, once the file browser is opened, the loop is expected a return from the browser.
I tried with the WScript after opening the browser but no luck.
If anyone has solution please suggest.
Sub File_Test()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim btnInput As MSHTML.IHTMLInputElement
Dim ie As Object
Dim pointer As Integer
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate "http://www.htmlquick.com/reference/tags/input-file.html"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = ie.document
Set HTMLButtons = HTMLDoc.getElementsByTagName("input")
For Each HTMLButton In HTMLButtons
For Each btnInput In HTMLButtons
If btnInput.Type = "file" Then
HTMLButton.Click
btnInput.Value = "C:\temp\test.txt"
pointer = 1
Exit For
End If
Next btnInput
If pointer = 1 Then Exit For
Next
End Sub
Here you go:
Check out: Excel upload a file to an FTP site
http://sam308.com/excel-upload-file-ftp-site

Get reference to Internet Explorer document object

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.

excel vba IE clicking a button

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

Resources