I want to add value in web page text box but getting object error.
I can't provide url as it will not work outside the office.
below html code:text box :
<input type="text" size="40" name="agent" id="go" value="">
Search :
<button type="submit">Submit</button>
VBA:
Sub login_page()
Dim ieApp As SHDocVw.InternetExplorer
Dim iedoc As MSHTML.HTMLDocument
IEApp.Visible = True
Set ieApp = New SHDocVw.InternetExplorer
ieApp.Visible = True
ieApp.navigate "http:/Login"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set iedoc = ieApp.document
with iedoc.forms(0)
.user.Value = "id"
.Password.Value = "Pass"
.submit
End With
ieApp.navigate "http:action=view"
Do While ieApp.Busy: DoEvents: Loop
'Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
ieApp.navigate "http:action=run"
Do While ieApp.Busy: DoEvents: Loop
ieApp.navigate "http:Report?"
action=run&category=interfaces&template=base%2Frouterprotocols§ion=5"
Do While ieApp.Busy: DoEvents: Loop
ieApp.document.parentWindow.execScript "window.location='Go?action=agent'"
End Sub
There is an id so use it
ieApp.document.getElementById("go")
Preceeding code
Dim ieApp As InternetExplorer
Set ieApp = New InternetExplorer
With ieApp
.Visible = True
.Navigate2 "URL"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementById("go").value = "xyz"
End With
let me help you.
Dim ie As New InternetExplorer
Dim elemnts As Object
Dim elem As Object
ie.Navigate url
Do While ie.ReadyState = 4: DoEvents: Loop 'Do While.
Do Until ie.ReadyState >= 3: DoEvents: Call aceitaAlerta(False): Loop 'Do Until
Set ELEMENTS = ie.Document.all
For Each elem In ELEMENTS
on error goto NextOne
If elem.name = "agente" And elem.id = "go" Then
elem.Value = "NEWVALUE"
elem.innerText = "NEWVALUE"
Exit For
End If
NextOne:
on error goto 0
Next elem
Related
I have a code to import HTML data from a website.
It works for importing data up to 94 Excel rows. After that it gives error
VBA Procedure too large.
I repeat the code started from ieApp.Navigate "https://icms.indianrail.gov.in/reports/ReportServlet?reportAction=Utility&reportType=LocoCurrStatus&subAction=main to End If for each row.
How do I modify the code for more than 600 rows?
Sub GetTable()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject
'create a new instance of ie
Set ieApp = New InternetExplorer
'you don’t need this, but it’s good for debugging
ieApp.Visible = True
'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://icms.indianrail.gov.in/reports/"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.UserId.Value = "88888"
.Password.Value = "******"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we’re in, go to the page we want
ieApp.Navigate "https://icms.indianrail.gov.in/reports/ReportServlet?
reportAction=Utility&reportType=LocoCurrStatus&subAction=main"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'get the table based on the table’s id
Set ieDoc = ieApp.Document
With ieDoc.forms(0)
.trainNo.Value = Sheets("sheet1").Range("B2").Value
.startDate.Value = Format(Date - 1, "dd-mmm-yyyy")
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.all.Item("TABLE_6")
'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "" & ieTable.outerHTML & ""
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("D2").Select
Sheet1.PasteSpecial "Unicode Text"
End If
End Sub
My Excel Sheet:
Is a long time ago to programming VBA. I want to fetch in a web Table all Linked PDF Files in coll um (x). First I have created the logins and navigate to the site to fetch file.
Site table:
Sub goToShopWH()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim clip As Object
'Table Grapping
Dim ieTable As Object
'create a new instance of ie
'Set ieApp = New InternetExplorer
Set ieApp = CreateObject("InternetExplorer.Application")
'you don’t need this, but it’s good for debugging
ieApp.Visible = True
'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://website.com/ishop/Home.html"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents:
Loop
' Login to site
Set ieDoc = ieApp.Document
'fill in the login form – View Source from your browser to get the control names
With ieDoc '.forms("loginForm_0")
.getElementById("loginField").Value = "username"
.getElementById("password").Value = "Password"
.getElementById("loginSubmit").Click
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we’re in, go to the page we want
'Switsh to search form
ieApp.Navigate "https://website.com/ishop/account/MyAccount,$comp$account$AccountNavigation.orderHistory.sdirect?sp=Saccount%2FOrderHistory"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
' fillout form
Set ieDoc = ieApp.Document
With ieDoc '.forms("Form_0")
.getElementById("PropertySelection_2").Value = "7"
.getElementById("commtxt").Value = "190055" 'Projekt Nummer oder Text.
.getElementById("Submit_0").Click
End With
Do
DoEvents
Loop Until ieApp.ReadyState = 4
Application.Wait Now + TimeSerial(0, 0, 5)
With ieDoc '.forms("Form_1")
.getElementById("PropertySelection").Value = "3"
.getElementById("PropertySelection").FireEvent ("onchange")
End With
Do
DoEvents
Loop Until ieApp.ReadyState = 4
Application.Wait Now + TimeSerial(0, 0, 5)
End With
Set webpage = ieApp.Document
Set table_data = webpage.getElementsByTagName("tr")
End Sub
Please help my to solve this to get I want import Table to sheet2 and download all PDF in ("Table"),("tbody")(tr)(1 to X),(td)(10),(href).click
I think you are after something like this.
Sub webpage()
Dim internet As Object
Dim internetdata As Object
Dim div_result As Object
Dim header_links As Object
Dim link As Object
Dim URL As String
Set internet = CreateObject("InternetExplorer.Application")
internet.Visible = True
URL = "https://www.google.co.in/search?q=how+to+program+in+vba"
internet.Navigate URL
Do Until internet.ReadyState >= 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 5)
Set internetdata = internet.Document
Set div_result = internetdata.getelementbyid("res")
Set header_links = div_result.getelementsbytagname("h3")
For Each h In header_links
Set link = h.ChildNodes.Item(0)
Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href
Next
MsgBox "done"
End Sub
Or, perhaps this.
' place your URL in cell L1
Sub HREF_Web()
Dim doc As HTMLDocument
Dim output As Object
Set IE = New InternetExplorer
IE.Visible = False
IE.navigate Range("L1")
Do
'DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set doc = IE.document
Set output = doc.getElementsByTagName("a")
i = 5
For Each link In output
'If link.InnerHTML = "" Then
Range("A" & i).Value2 = link
' End If
i = i + 1
Next
MsgBox "Done!"
End Sub
I tried the code below. Most of the execution time was stuck in on error goto 0 and no success in further steps .fireevent ("onchange"). Is there some way I can optimize the process better?
Public Sub makeselections()
Dim ie As New InternetExplorer, var As String, ele As Object
var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).value
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").value = var
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Do
On Error Resume Next
Set ele = .querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']")
On Error GoTo 0
Loop While ele Is Nothing
.querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']").Selected = True
.querySelector(".financials select").FireEvent "onchange"
End With
End With
End Sub
It is presumably stuck as ele remains Nothing i.e. ticker is not found or at least that href value isn't found. Use a timed loop to allow for exit
Option Explicit
Public Sub MakeSelections()
Dim ie As New InternetExplorer, var As String, ele As Object, t As Date
Const MAX_WAIT_SEC As Long = 10
var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).Value
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").Value = var
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
t = Timer
Do
On Error Resume Next
Set ele = .querySelector("[value$='quarter']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
ele.Selected = True
.querySelector(".financials select").FireEvent "onchange"
End With
End With
End Sub
I've created this code that simulates my actual application. And I'm trying to run the procedure through WebBrowser1. If in my actual application it was possible to use .document.getElementById would work, but you need .getElementsByClassName or TagName. How could I do this with the data below?
The code application is executed by the CommandButton and the WebBrowser will be inside a Userform.
Private Sub CommandButton1_Click()
Dim IE As InternetExplorer
Dim iELE As Object
Dim xUser As String
Dim xPswd As String
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate ("https://twitter.com/login?lang=pt")
xUser = "your username"
xPswd = "your password"
Application.Wait (Now + TimeValue("0:00:10"))
Do While IE.Busy = True Or IE.ReadyState <> 4: DoEvents: Loop
For Each iELE In IE.Document.getElementsByClassName("js-username-field email-input js-initial-focus")
If iELE.Value = "" Then
iELE.Value = xUser
End If
Exit For
Next
For Each iELE In IE.Document.getElementsByClassName("js-password-field")
If iELE.Value = "" Then
iELE.Value = xPswd
End If
Exit For
Next
Application.Wait (Now + TimeValue("0:00:05"))
For Each iELE In IE.Document.getElementsByClassName("submit EdgeButton EdgeButton--primary EdgeButtom--medium")
If iELE.Value = "" Then
iELE.Click
End If
Exit For
Next
End Sub
Try this
Sub Login(MyLogin, MyPass, strUrl)
Dim IeApp As InternetExplorer
Dim IeDoc As Object
Dim ieTable As Object
Set IeApp = New InternetExplorer
IeApp.Visible = False
IeApp.Navigate strUrl
Do While IeApp.Busy: DoEvents: Loop
Do Until IeApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc = IeApp.Document
With IeDoc
.getElementsByClassName("js-username-field")(0).value = MyLogin
.getElementsByClassName("js-password-field")(0).value = MyPass
End With
IeDoc.getElementsByClassName("submit")(0).Click
Do While IeApp.Busy: DoEvents: Loop
Do Until IeApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
End Sub
https://www.pk-anexcelexpert.com/vba-web-browser-in-the-user-form/
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