Fetching text from website using VBA for Mac - excel

I am sorry if this is a very basic question, but I'm really desperate. I want to fetch the output of the website https://cactus.nci.nih.gov/chemical/structure/78-70-6/smiles, and input it into an Excel cell using VBA. Does anyone know of a simple and straightforward way of doing this?
In a way, I would like something like "curl", in bash, but that could be used in VBA for Excel.
I'm using Excel for Mac, Version 16.16.14, with VBA 7.1.
I've tried many of the approaches suggested online, and nothing seemed to work on my version of Excel.
Thanks a lot.

This should do the trick;
Dim InternetExplorer As Object 'dim your internet explorer
Dim WebsiteURL As String: WebsiteURL = "https://cactus.nci.nih.gov/chemical/structure/78-70-6/smiles" 'dim and set your website url
Dim WebsiteText As String 'dim a variable to store the website text
Set InternetExplorer = CreateObject("InternetExplorer.Application") 'set your internet explorer
With InternetExplorer
.Visible = 1 'set to show (if you put 0 the browser would be hidden - just remember to set to nothing at end)
.navigate WebsiteURL 'navigates to websiteurl
While .Busy Or .readyState <> 4 'waits for page to load
DoEvents
Wend
End With
Dim WebPage As HTMLDocument 'dim your webpage as htmldocument
Set WebPage = InternetExplorer.document 'set your webpage to the internet explorer
WebsiteText = WebPage.body.innerText: Range("A1").Value = WebsiteText 'puts website text into variable : write to whatever cell you want
EDIT:
Just noticed you said for MAC - My bad as this uses internet explorer

Related

VBA extract text value from webpage?

I have a webpage with some text in a HTML Span like so:
<span id="ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate">Expiry Date : 16/02/2018</span>
I am trying to get this value display it as a message in excel using the below code:
Sub PullExpiry()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://www.brcdirectory.com/InternalSite//Site.aspx?BrcSiteCode=" & Range("J6").Value
.Visible = True
End With
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set getPrice = appIE.Document.getElementById("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate")
Dim myValue As String
myValue = getPrice.innerText
appIE.Quit
Set appIE = Nothing
MsgBox myValue
End Sub
This was working on my laptop (operating windows) but it does not work on my computer (also operating windows). Both windows are the same version with the same version of IE. I cannot explain it.
I have Microsoft Office and Excel Object libraries turned on in both references.
I get an error about an active x component not being able to create something
Please can someone show me where i am going wrong?
You need to add Microsoft Internet Controls to your references to be able to create the object. You may have to register the shdocvw.dll library on your computer. It may be registered on your laptop already which is why it might be bombing on your computer.
How to register a .dll
MSDN Documentation: look at the last sentence before the C# example.
Similar Question
After playing around a little bit and ensuring everything was registered, this ran fine on my PC:
Public Sub ie()
Dim ieApp As SHDocVw.InternetExplorerMedium
Dim html As HTMLDocument
Set ieApp = New SHDocVw.InternetExplorerMedium
ieApp.Visible = True
ieApp.Navigate "http://internalAddress/"
Do While ieApp.Busy Or ieApp.ReadyState <> 4
DoEvents
Loop
Set html = ieApp.Document.getElementById("myID")
End Sub

VBA code doesn't work consistantly

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

Web page connection through VBA excel

I am trying to make a currency calculater in VBA excel, and I cannot make it work. My code below show how far I am. My problem is that I cannot get the calculated number on the web page into excel (the sDD). This code only concerns from DKK to USD, which I will change later, but right now the problem is to get the exchange amount into Excel. Hope you can help!
code:
Sub currency_1()
Dim Price As Double
Dim IE As New InternetExplorer
Price = Range("C4")
IE.Visible = True
IE.Navigate "https://finance.yahoo.com/currency-converter/#from=DKK;to=USD;amt=" & Price
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.Document
On Error Resume Next
Dim sDD As Double
sDD = Doc.getElementById("yui_3_18_1_1_1467628123397_410").Value
IE.Quit
Range("E4").Value = sDD
End Sub
Alright, I'm pretty sure your problem is I cannot find the "yui_3_18_1_1_1467628123397_410" on the source code for that website.
Your On error resume next is really bad coding and will hide this issue. Remove it, run it again and see if it bugs out

Keeping a record of automatically updated price quotes from web using VBA and Excel

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

Copy from excel and paste into online form

Firstly may I apologise for the fact that I am not the best with computers,
I have quite a large excel sheet for which I have to copy and paste 10 columns per row into an online form.
Is it possible to create a macro for this and if yes how can I do so.
Also please keep in mind that I am at work, so I'm limited from the use of plugins and I unfortunately have to use internet explorer.
Your help would be much adored,
Thank You
Yes it is possible. You can use IE to automate this using vba.
Sub xtremeExcel()
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
sURL = "*******Paste URL Here************"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.navigate sURL
oBrowser.Visible = True
Do
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.document
'Use firebug to identify the textbox or object you need. and you can set its value like
'HTMLDoc.GetElementByID("abcd").value="xyz"
End Sub

Resources