Any one of three highlighted part are the value i want to print. I am trying below code
Sub JJ()
Dim IE As New SHDocVw.InternetExplorer
Dim hdoc As MSHTML.HTMLDocument
Dim ha As String
IE.Visible = True
IE.navigate "https://www.nseindia.com/get-quotes/equity?symbol=DIVISLAB"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set hdoc = IE.document
ha = hdoc.getElementById("preOpenFp").innerText
Debug.Print ha
End Sub
But the output is nothing pls help.
The website you're trying to scrape offers a very convenient way to do it. All you need to do is send an HTTP request and get the corresponding JSON response which looks like so:
If you take a look at the network traffic in your browser's developer tools, you'll see the requests that are being sent to the server when the page is being loaded. Among these requests you'll find the following one:
To send this request and get the info you need, you have to do the following:
Option Explicit
Sub nse()
Dim req As New MSXML2.XMLHTTP60
Dim url As String
Dim json As Object
url = "https://www.nseindia.com/api/quote-equity?symbol=DIVISLAB"
With req
.Open "GET", url, False
.send
Set json = JsonConverter.ParseJson(.responseText)
End With
Debug.Print json("preOpenMarket")("IEP")
End Sub
This will print the value of IEP to your immediate window (in this case 2390). You can modify the code to best fit your needs.
To parse a JSON string you will need to add this to your project. Follow the installation instructions in the link and you should be set to go.
You will also need to add the following references to your project (VBE>Tools>References):
Microsoft XML version 6.0
Microsoft Scripting Runtime
I'm trying to access the document object of a frame with JS using WebBrowser. The code below works perfectly, but I need to use WebBrowser instead.
This is the error I've got:
Run-time error '438'
Object doesn't support this property or method
Any guidance please?
Sub GrabWorkerId1()
Dim objIE As InternetExplorerMedium
Set objIE = New InternetExplorerMedium
objIE.Visible = True
objIE.Navigate "my url"
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Open "c:\temp\GrabWorkerId2.log" For Output As #3
Write #3, objIE.Document.getElementsbyTagName("iframe")(1).contentDocument.getElementsbyTagName("select")(0).innerhtml
Close 3
Set objIE = Nothing
End Sub
Object doesn't support this property or method
Perhaps the issue is related the Close method and the related method to access the html elements.
First, please try to use F12 developer tools to check whether the website contains the iframe tag and the select element, and check the account. According to your code, the page should contain at least two iframe tag and the iframe content page will contains at least one select element, please verify it. Also, you could add a debugger from the VBA script, and check the innerhtml value, make sure you could get the value.
Second, about the Close method, try to modify the code as below:
Close #3
Besides, please check the following code, I have tested it on my side, it works well (the website contains one iframe tag, and the iframe content page just contains one select element):
Sub GrabWorkerId1()
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate "<website url>"
While objIE.ReadyState <> 4
DoEvents
Wend
Open "d:\temp\test.log" For Output As #3
Write #3, objIE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementsbyTagName("select")(0).innerhtml
Close #3
Set objIE = Nothing
End Sub
I am trying to scrape IPO date from crunchbase.
Unfortunately I get Runtime Error 1004 “Application-defined or Object-defined error”.
My goal is to save IPO date in A1 cell.
Sub GetIE()
Dim IE As Object
Dim URL As String
Dim myValue As IHTMLElement
URL = "https://www.crunchbase.com/organization/verastem"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate URL
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
Set myValue = IE.Document.getElementsByClassName("post_glass post_micro_glass")(0)
Range("A1").Value = myValue
Set IE = Nothing
End Sub
I can't find that class name in the html for that url. You can use the css selector I show below which can be scraped by xmlhttp and thus avoiding opening a browser
Option Explicit
Public Sub GetDate()
Dim html As HTMLDocument
Set html = New HTMLDocument '< VBE > Tools > References > Microsoft Scripting Runtime
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.crunchbase.com/organization/verastem#section-overview", False
.send
html.body.innerHTML = .responseText
End With
ActiveSheet.Range("A1") = html.querySelectorAll(".field-type-date.ng-star-inserted").item(1).innerText
End Sub
If you don't want to use compound classes then you can also use
ActiveSheet.Range("A1") = html.querySelectorAll("#section-ipo-stock-price .field-type-date").item(1).innerText
You can see the relevant html here:
Note the element has multiple (compound) classes
<span class="component--field-formatter field-type-date ng-star-inserted" title="Jan 27, 2012">Jan 27, 2012</span>
There are 3 classes component--field-formatter ; field-type-date and ng-star-inserted. I use two of these in combination in the first solution I give. Multiple classes is popular now-a-days due to the versatility it gives in page styling e.g. it allows overriding styles easily. You can read about css specificity* to understand this better.
More classes may mean the code is a little less robust as the ordering of classes may be changed and a class, or more, may be removed. This was raised by #SIM in a comment on an answer to another web-scraping question. Thus, I offer one solution with two of the classes used, and another solution with only one of the classes used.
Whilst you do get the same date for this page with simply:
ActiveSheet.Range("A1") = html.querySelector("#section-ipo-stock-price .field-type-date").innerText
I wouldn't want to assume that would always hold true as it grabs the date from the line where it says "Their stock opened".
* https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
References:
querySelectorAll
css selectors
I want to write a program using Visual Basic that can automatically import data from yahoo finance (price quotes) to an Excel sheet and update every minute. I know how to do this, though,the thing that I’m struggling with is that when excel updates the prices, the previous prices will be erased as Excel re-writes the new prices on the same cells; in other words, I cannot keep a record of quoted prices as time goes by. I wonder if it is possible that the program can write the new prices in different cells as it updates.
I am trying to get prices from this source: https://au.finance.yahoo.com/q?s=AUDUSD=X
I assume you are using the front end, currently, to pull data from the web. Instead consider using the Internet Explorer object to fetch the HTML from the page:
'Declare IE object
Dim IE As Object
'Using late binding (instead of tools>>references so you don't get stuck on a version)
'Open IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
'Navigate to your web page so we can get the html
IE.Navigate "https://au.finance.yahoo.com/q?s=AUDUSD=X"
'Wait until its ready to go
Do While IE.busy
'Update the statusbar that we are waiting
Application.StatusBar = "Fetching stock price from yahoo"
DoEvents
Loop
'reset the statusbar
application.statusbar = False
And then use the Microsoft HTML Object Library (in Tools>>References) to parse the html results.
'Declare an HTML document
Dim html As HTMLDocument
'Drop html from IE to the HTMLDoc
Set html = IE.document
'Kill IE, we don't need it anymore
Set ie = Nothing
'Parse the HTML doc...
Dim objElement As HTMLObjectElement
Set objElement = html.getElementById("yfs_l10_audusd=x")
Sheet1.Range("A1").Value = objElement.innerText
Once you get the value you need from the web page you can stick it where ever you like. I have stuck it in Range A1 of Sheet1
I am using Excel VBA code to click a button on our website. I know this isn't the best of things to be doing, but it is the least objectionable option available to me.
I can using this code, successfully load imdb.com, google, etc. But when I load our local site, I lose control of the ie object, I can't check readyState, I can't Quit.
Here is the error I get.
Run-time error '-2147023179 (800706b5)':
Automation error
The interface is unknown
Every so often I instead get this message:
Run-time error '-2147417848 (80010108)':
Automation error
The object invoked has disconnected from its clients.
Clicking Debug indicates the ie.readyState, I commented that out and then it points to ie.Quit
Sub dothestuff()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://www.google.com/"
anerror = webload(ie)
ie.Quit
Set ie = Nothing
End Sub
Function webload(ie)
Do Until ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
End Function
Here is a quick and easy solution for this issue:
Instead of:
set IE = createobject("internetexplorer.application")
Use:
Set IE = New InternetExplorerMedium
No need to tweak the IE settings
Here's what's happening. When your browser internally "jumps" to a different security zone - from say a local address to an inTRAnet address or to an inTERnet address - IE closes/drops the current process of IE and opens another one with tighter security. The old and new IE processes are IE child processes to the parent IE task so you don't see it happen either by watching the browser or watching the process in Task Manager. If you use Process Explorer (free from Microsoft), you can see this happen.
What you need to do in this type of environment is use Anup Upadhyay's solution above. His code snippet looks at all IE tasks (parent and child doesn't make a difference) and finds the new IE process that is pointing to the web address that the original process was given. As long as you don't have multiple browsers open to the same address, it will find the new process reliably and carry on like you would expect it to.
Note: You might also need to use the InternetExplorerMedium object as opposed to the InternetExplorer object. It is also better at "holding on" to a session.
Here's my version which is almost the same thing as Anup's.
References for MS Office VBA:
Microsoft Internet Controls
Microsoft Shell Controls and Automation
n
Dim IE As InternetExplorerMedium ' This object (the "medium" variety as opposed to "InternetExplorer") is necessary in our security climate
Dim targetURL As String
Dim webContent As String
Dim sh
Dim eachIE
targetURL = "[your URL here]"
Set IE = New InternetExplorerMedium
IE.Visible = False ' Set to true to watch what's happening
IE.Navigate targetURL
While IE.Busy
DoEvents
Wend
Do
Set sh = New Shell32.Shell
For Each eachIE In sh.Windows
If InStr(1, eachIE.LocationURL, targetURL) Then
Set IE = eachIE
'IE.Visible = False 'This is here because in some environments, the new process defaults to Visible.
Exit Do
End If
Next eachIE
Loop
Set eachIE = Nothing
Set sh = Nothing
While IE.Busy ' The new process may still be busy even after you find it
DoEvents
Wend
Try this one:
Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
We had this problem using IE9. It arises because of security settings. There is a setting called "Enable Protected Mode" on the "Security" tab of the "Internet Options" dialog box (tools...internet options). Each "zone" can have a different setting for "enable protected mode" We unchecked "enable protected mode" and this solved our problem. I think what is happening is that when switch to a zone that is in protected mode, IE starts a new process to handle traffic in that zone (maybe killing the old process). When this happens your IE object variable in VBA gets disconnected from Internet Explorer.
Alright, just found a solution, decided out of desperation to try loading 127.0.0.1 instead of localhost, sure enough, no problems, so resolved the ip address of the local intranet server, and now I am good to go.
I don't really understand why, but this has solved my issue.
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate "C:\abc.html"
'We are getting error here , I have found a small tricky alternate solution for this as below
'Take reference for Microsoft Shell Controls And Automation
'Use below code to reassign IE object for the lost connection issue
Dim sh
Dim eachIE
Do
Set sh = New Shell32.Shell
For Each eachIE In sh.Windows
' Check if this is the desired URL
' Here you can use your condition except .html
' If you want to use your URL , then put the URL below in the code for condition check.
' This code will reassign your IE object with the same reference for navigation and your issue will resolve.
'
If InStr(1, eachIE.locationurl, ".html") Then
Set IE = eachIE
Exit Do
End If
Next eachIE
Loop
For me, the following worked:
Open Internet explorer
Go to tools
Goto Internet options
Switch to Security tab
Click Trusted sites
Click sites
You will see the site url, select it
Click Remove
Click close, apply and Ok
Ps. I tested these steps on IE 8 browser.
Maybe you're having a problem with binding? That's a strange error to get for this.
Try this code (adapted from http://www.excely.com/excel-vba/ie-automation.shtml). Hopefully it helps:
Option Explicit
' lasts for the life of Excel being open
Dim ie As Object
Sub dothestuff()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://www.google.com/"
Do While ie.Busy
DoEvents
Loop
ie.Quit
Set ie = Nothing
End Sub
I am having the exact same issue working with a website that is inside our company intranet and is also included in my trusted sites. Disabling protected mode allows me to check ReadyState, but only if I disable for the internet zone in addition to intranet and trusted sites.
Why would enabling protected mode for the internet zone cause the automation error when checking ReadyState on a trusted site? Is there a way around this without disabling protected mode for the internet zone?
Another solution..
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate ("http://URLOnMyIntranet/site.html")
IE.Visible = True
Set IE = nothing
Set objShellApp = CreateObject("Shell.Application")
For Each objWindow In objShellApp.Windows
Debug.Print objWindow.LocationName
If LCase(objWindow.LocationName) = LCase("Your windows name, use debug to find out") Then
Set IE = objWindow
End If
Next
Note, using InternetExplorerMedium (see other answers) usually all you need, FYI also you could use Url property obj.LocationURL instead of window name which would be more accurate. See other answers.
Dim IE As InternetExplorer
Set IE = New InternetExplorerMedium
IE.Navigate "yousite.org"
IE.Visible = True
Do While IE.Busy
DoEvents
Loop
Set HTML = IE.document
Do
var8 = HTML.DocumentElement.innerHTML
v3 = var8
v4 = InStrRev(v3, "the class or ID you are looking for when the _
page final loads", -1)
Loop While v4 = 0
Do While IE.Busy
DoEvents
Loop
'Your in the new page look for elements and click rinse and repeat
'the Do loop. This method works with my intranet corporate site