I have a problem with entering data from Excel into a web browser using the code below. He reports error number 438 to me and I don't know how to solve it.
The error appeared here:
iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
This is my full code:
Sub provera_menice()
Dim wb As Workbook
Set wb = ThisWorkbook
'Dim rgMB As Range
'Set rgMB = Application.InputBox("Izabrati MB klijenta koji se deblokira.", Type:=8)
'Dim r As Long
'Dim k As Long
'r = rgMB.Row
'k = rgMB.Column
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/duznici-pn/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim ieDoc As MSHTML.HTMLDocument
Set ieDoc = objIE.document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = ieDoc.frames(0).document
'in the search box put some cell value
'MB: <input class="form-control" name="matbr" type="text" tabindex="4" size="13" maxlength="8" value="">
iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
'search: <input class="btn" type="submit" name="send" id="send" value="????????" tabindex="8">
iframeDoc.getElementById("send").Click
End Sub
I searched for a solution on the net, but I can't fix the error.
I try to set loop, but nothing entry.
Set x = iframeDoc.getElementsByName("matbr")
For i = 0 To x.Length
If x(i) = "matbr" Then
'in the search box put some cell value
'MB: <input class="form-control" name="matbr" type="text" tabindex="4" size="13" maxlength="8" value="">
iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
'search: <input class="btn" type="submit" name="send" id="send" value="????????" tabindex="8">
iframeDoc.getElementById("send").Click
End If
i = i + 1
Next
I found a solution, thank you all!
Here is the correct code:
Sub provera_menice()
Dim wb As Workbook
Set wb = ThisWorkbook
'Dim rgMB As Range
'Set rgMB = Application.InputBox("Izabrati MB klijenta koji se deblokira.", Type:=8)
'Dim r As Long
'Dim k As Long
'r = rgMB.Row
'k = rgMB.Column
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/duznici-pn/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Dim ieDoc As MSHTML.HTMLDocument
Set ieDoc = objIE.document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = ieDoc.frames(0).document
iframeDoc.getElementsByName("matbr")(1).Value = Range("a2").Value
iframeDoc.getElementById("send").Click
Range("b4").Value = iframeDoc.getElementsByClassName("table_text cell")(5).textContent
End Sub
Related
I wrote some code to scrape data from a website. I've tested it on 5 difference machines with different versions of excel and it all works fine. But on the intended users machine we get type mismatch error.The code fails at the last line below.
Sub LogIn()
Dim ie As SHDocVw.InternetExplorer
Dim iDoc As MSHTML.HTMLDocument
Dim ele As MSHTML.IHTMLElement
Dim eles As MSHTML.IHTMLElementCollection
Dim tableSection As MSHTML.IHTMLElement
Dim tableRow As MSHTML.IHTMLElement
Dim tableCell As MSHTML.IHTMLElement
Dim smallCell As MSHTML.IHTMLElement
Dim iCol As Integer
Dim iRow As Integer
Dim iCounter As Integer
iRow = 0
Do
iRow = iRow + 1
Loop Until Cells(iRow, 5) = ""
Range(Cells(1, 5), Cells(iRow, 6)).ClearContents
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate ("https://www.howdidido.com/")
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set iDoc = ie.document
any help greatly appreciated.
I have tried the following code and it is working alright. Maybe it can help you (seems as two loops and doEvents are needed for the ready state completes).
Dim iDoc As MSHTML.HTMLDocument
Dim iCol As Integer
Dim iRow As Integer
Dim iCounter As Integer
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://www.automateexcel.com/excel/"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
Set iDoc = IE.Document
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
I´m trying to get the number part of an Euro amount string from a website. I tried different thing things, that normally work. here are my 3 best attempts. nothing worked
My first attempt:
Sub gsellor()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
Dim HTMLPreisTag As IHTMLElement
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set HTMLPreisTag = objIE.document.getElementById("page_content").Children(0).Children(1).Children(0).Children(1).Children(0)
Debug.Print HTMLPreisTag.className
Debug.Print HTMLPreisTag.Children(1).Children(0).Children(0).innerText ' gets stuck right here
objIE.Quit
End Sub
the immediate window shows:
MarketInfo_market-info_3lkUj
My second attempt:
Sub gsellor()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
Dim HTMLPreisTag As IHTMLElement
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set HTMLtags = objIE.document.getElementsByTagName("span")
Debug.Print HTMLtags.Length
For Each HTMLtag In HTMLtags
Debug.Print "name: " & HTMLtag.className
Debug.Print "text: " & HTMLtag.innerText
Next HTMLtag
objIE.Quit
End Sub
the immediate window shows:
3
name:
text:
name: AccountPanel_account-panel-user-name_2TNIL
text:
name: sr-only
text: Toggle navigation
My third attempt:
Sub gsellor()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
Dim HTMLPreisTag As IHTMLElement
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set HTMLtags = objIE.document.getElementsByClassName("MarketInfo_market-num_1lAXs")
Debug.Print HTMLtags.Length
For Each HTMLtag In HTMLtags
Debug.Print "name: " & HTMLtag.className
Debug.Print "text: " & HTMLtag.innerText
Next HTMLtag
objIE.Quit
End Sub
immediate window:
0
To get the content you need to make your browser wait until the content is not nothing. This is another way to serve the same purpose:
Sub gsellor()
Dim ObjIE As New InternetExplorer
Dim Ohtml As HTMLDocument
Dim item_price As Object
With ObjIE
.Visible = True
.navigate "https://www.gdax.com/trade/LTC-EUR"
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set Ohtml = .document
End With
Do
Set item_price = Ohtml.getElementsByClassName("MarketInfo_market-num_1lAXs")(0)
DoEvents
Loop While item_price Is Nothing
MsgBox item_price.innerText
ObjIE.Quit
End Sub
This is not for the first time I am mentioning this...but yeah I am a rookie in vba and in need of help...
I am having trouble in filling up a value in a box in webpage using vba excel.
The source code of the box is
<input
name="MultiRenameTable_OR:wt.epm.EPMDocument:3053059700_NEWNAME_JSID"
class="enabledfield"
id="MultiRenameTable_OR:wt.epm.EPMDocument:3053059700_NEWNAME_JSID"
style="WIDTH:98% onblur="setJSAttributeValue(this,'NEWNAME_JSID','MultiRenameTable', 'epmdoc_09876889_u46_drw',undefined)" type="text" size="25" ?=""
trlId="MultiRenameTable_epmdoc_09876889_u46_drw_NEWNAME_JSID"></input>
the code i wrote is
Sub xx()
Dim ie As Object
Dim doc As HTMLDocument
Dim lo As IHTMLElementCollection
Dim l As IHTMLElement
Set ie = CreateObject("internetexplorer.application")
ie.navigate "mysiteurl"
ie.Visible = True
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4: DoEvents: Loop
Set doc = ie.document
doc.getElementById("MultiRenameTable_OR:wt.epm.EPMDocument3053059700_NEWNAME_JSID").Value = "xyz"
End Sub
It doesnt set the value.
Also i cannot use "name","ID","trlID","onblur" because they keep on changing each time i launch IE.
Please help me with this.
I'm guessing from the setJSAttributeValue that the name will always contain the substring "NEWNAME_JSID". Just grab all of the input elements, then loop through them looking for it in the name:
Sub xx()
Dim ie As Object
Dim doc As HTMLDocument
Dim lo As IHTMLElementCollection
Dim l As IHTMLElement
Set ie = CreateObject("internetexplorer.application")
ie.navigate "mysiteurl"
ie.Visible = True
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4: DoEvents: Loop
Set doc = ie.document
Set lo = doc.getElementsByTagName("input")
For Each l In lo
If l.Name Like "*NEWNAME_JSID*" Then
'Do whatever.
Exit For
End If
Next l
End Sub
I am working on a code that uses VBA-excel to navigate to a website. Once I have logged into the website, I am not able to interact with any of the html elements within the page. I need to be able to click on some check boxes and enter a date range into a field. Here is the code that I have. It works when I open a new window but I cant get it to work within the original ie window. I would like this process to be done in one window if possible. Any feedback is greatly appreciated.
Private Sub CommandButton1_Click()
Dim yesterday As String
yesterday = Date - 1
Dim RowCount As Integer
Dim x As Integer
Dim nik As Integer
'Opens Internet Explorer
Dim Shellwins As SHDocVw.ShellWindows
Dim Shellie As SHDocVw.InternetExplorer
Set Shellwins = New SHDocVw.ShellWindows
Dim ie1 As InternetExplorer
Strurl = "father.com"
Set ie1 = New InternetExplorer
ie1.Visible = True
ie1.navigate Strurl
Do While ie1.readyState <> 4 And ie1.Busy
DoEvents
Loop
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
'MsgBox htmlel.Name
If htmlel.ID = "ctl00_Body_txtEmailAddress" Then
htmlel.Value = "dad#dad.com"
End If
Next
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
'MsgBox htmlel.Name
If htmlel.ID = "ctl00_Body_txtPassword" Then
htmlel.Value = "mom"
End If
Next
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_btnLogon" Then
htmlel.Click
End If
Next
Strurl = "url.com"
Set ie2 = New InternetExplorer
ie2.Visible = True
ie2.navigate Strurl
ie1.Quit
Do While ie2.readyState <> 4 And ie2.Busy
DoEvents
Loop
Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_txtStartDate" Then
htmlel.Value = yesterday
End If
Next
Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_txtEndDate" Then
htmlel.Value = yesterday
End If
Next
Below is is an HTML form and below that, is a vb procedure, "LoginExamp" that enters in the username and password. I am unable to locate the button however and click it since it does not seem to show up as a mshtml.HTMLInputElement. "htmlInput.click()" never runs. How can I adjust the loginExamp code so that the button is clicked. Thanks for any help.
<form id="loginform" name="loginform" method="post" action="">
<input id="username" class="formfield" type="text" value="User Name" maxlength="40" name="Xusername">
<input id="password" class="formfield" type="password" onfocus="clearDefault(this)" maxlength="40" name="Xpassword">
<button class="subButton" onclick="javascript: submitform()">submit!</button>
</form>
With the below code
Public Sub loginExamp()
Dim objIE As SHDocVw.InternetExplorer
Dim htmlDoc As mshtml.HTMLDocument
Dim htmlInput As mshtml.HTMLInputElement
Dim htmlColl As mshtml.IHTMLElementCollection
Dim url As Object
url = "http://localhost/ButtonClickTest.html" 'just a test page with the loginform above
objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate(url)
.Visible = True
While .Busy = True
Threading.Thread.Sleep(2000)
End While
htmlDoc = .Document
htmlColl = htmlDoc.getElementsByTagName("INPUT")
While .Busy = True
Threading.Thread.Sleep(2000)
End While
For Each htmlInput In htmlColl
If htmlInput.name = "Xusername" Then
htmlInput.value = "theusername" 'this works
ElseIf htmlInput.name = "Xpassword" Then
htmlInput.value = "thepassword" 'This works too
End If
If htmlInput.className = "subButton" Then 'This is never true
htmlInput.click() 'This line never runs
End If
Next htmlInput
End With
End Sub
I was able to finally figure out the answer to my own question. Below is the code that works. Both the username and password is entered and also the button is clicked.
Public Sub loginExamp()
Dim objIE As SHDocVw.InternetExplorer
Dim htmlDoc As mshtml.HTMLDocument
Dim htmlInput As Object
Dim url As String
url = "http://localhost/N1/ButtonClickTest.html"'test page with the loginform above
objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate(url)
.Visible = True
While .Busy = True
Threading.Thread.Sleep(2000)
End While
htmlDoc = .Document
Dim htmlColl As mshtml.HTMLFormElement = DirectCast(htmlDoc.forms.item("loginform"), mshtml.HTMLFormElement)
While .Busy = True
Threading.Thread.Sleep(2000)
End While
For Each htmlInput In htmlColl
If htmlInput.name = "Xusername" Then
htmlInput.value = "theusername"
ElseIf htmlInput.name = "Xpassword" Then
htmlInput.value = "thepassword"
End If
If htmlInput.className = "subButton" Then
htmlInput.click()
End If
Next htmlInput
End With
End Sub