Web page connection through VBA excel - 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

Related

Need help amending macro to use one Internet Explorer Window

The below function opens Oanda.com and takes the currency conversion rate between USD and a input (another currency). The equation will be filled down with another macro to span 48 rows. The macro as it stands, will open 48 Internet Explorer Windows and close them after extracting the data point (1 at a time as the equation updates down the column). This process is tedious and the below method seems to be more efficient on every front but I cannot figure out how to implement:
Is there a way to amend this to first check for existing Internet Explorer window, If one is open, simply use that window to go to the domain (which is variable here) and extract data. If a window is not open, then open one. I do not know procedures to search for programs running in the background.
The main goal is to speed up the equation when it is executed by fill down. Any suggestions welcome.
Clarity Edit: I created the UDF to help me build a table. Currency tickers (USD, EUR, GBP, etc) will span Column A down to row 48. Column B needs to show the corresponding conversion rate matched against 1 USD (down to row 48). The UDF below does as intended but i'm seeking an alternative, more efficient, way to do this.
Option Explicit
Public Function ConvertUSD(ConvertWhat As String) As Double
'References
' Microsoft XML, vs.0
' Microsoft Internet Controls
' Microsoft HTML Object Library.
Dim IE As New InternetExplorer
'IE.Visible = True
IE.Navigate "https://www.oanda.com/currency/converter?quote_currency=USD&base_currency=" & ConvertWhat
Do
DoEvents
Loop Until IE.ReadyState = ReadyState_Complete
Dim Doc As HTMLDocument
Set Doc = IE.Document
Dim Ans As String
Ans = Trim(Doc.getElementsByTagName("tbody")(2).innerText)
Dim AnsExtract As Variant
AnsExtract = Split(Ans, " ")
ConvertUSD = AnsExtract(4)
IE.Quit
End Function
Try this:
Sub MainSub()
Dim IE As InternetExplorer
Set IE = New InternetExplorer
'
Dim x As Long
Dim Currencies As Variant
Currencies = Array("GBP", "EUR", "JPY", "HKD")
'
For x = LBound(Currencies) To UBound(Currencies)
Debug.Print "1 USD = " & ConvertUSD(Currencies(x), IE) & " " & Currencies(x)
Next x
IE.Quit ' Quit here instead
Set IE = Nothing
End Sub
Public Function ConvertUSD(ByVal ConvertWhat As String, IE As InternetExplorer) As Double
'References
' Microsoft XML, vs.0
' Microsoft Internet Controls
' Microsoft HTML Object Library.
' Dim IE As New InternetExplorer ' Commented out here
IE.Navigate "https://www.oanda.com/currency/converter?quote_currency=USD&base_currency=" & ConvertWhat
Do
DoEvents
Loop Until IE.ReadyState = ReadyState_Complete
Dim Doc As HTMLDocument
Set Doc = IE.Document
Dim Ans As String
Ans = Trim(Doc.getElementsByTagName("tbody")(2).innerText)
Dim AnsExtract As Variant
AnsExtract = Split(Ans, " ")
ConvertUSD = AnsExtract(4)
' IE.Quit ' Don't quit here
End Function
Your problem is that you keep opening a new IE every time the function is called. However if you open one before calling the function, you'll be able to re-use it as needed - and only quit after you've finished.

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: It only shows ID at Ispect Element

I'm a newbie at VBA programming, and I use it to build macro a in excel. Here is this webpage: http://www.eppraisal.com/home-values/property/14032-s-atlantic-ave-riverdale-il-60827-59143864/
I want to extract the value of $52,920 on the right upper corner. When I select the Inspect Element, this is the following:
<p id="eppraisalval">$52,920</p>
I checked the Page Source, but I couldn't find it. The closest thing I found:
<span id="eppraisal_val" class="valuation-estimates-price ajaxload" data-url="/home-values/property_lookup_eppraisal?a=14032%20S%20Atlantic%20Ave&z=60827&propid=59143864">loading...</span>
I tried the get getElementById("eppraisal_val") and getElementById("eppraisalval"), but none of those worked.
How can I address my code to get that element?
Here is more from the inspect element window, aroun that code:
<span id="eppraisal_val" class="valuation-estimates-price ajaxload" data-url="/home-values/property_lookup_eppraisal?a=14032%20S%20Atlantic%20Ave&z=60827&propid=59143864"><p id="eppraisalval">$52,920</p><p class="main-page-description-small valuation_details" style="display:none;margin-top:5px">Low: $44,982 <br>High: $60,858</p></span>
Here is the shorter version of code I tried:
Sub macroID()
On Error Resume Next
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
Dim doc As HTMLDocument
Dim valuation As String
Dim val1 As String, val2 As String
Set doc = ie.Document
ie.Visible = True
ie.navigate "http://www.eppraisal.com/home-values/property/14032-s-atlantic-ave-riverdale-il-60827-59143864/"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.Document
valuation = doc.getElementById("eppraisalval")
Cells(1, 4).Value = valuation
Application.Wait (Now + TimeValue("0:00:03"))
End Sub
The issue with scraping from that URL is that there is a verification page before hand which requires you to confirm you "are not a robot" which makes it hard to scrape anything from it.
If you manually do this first it may save this in your cache and then allow you to run macros freely to scrape the website however you'd have to try this out.
In the meantime, the only issue I could see with your code is that you haven't included .innertext after .getElementById("eppraisalval"). The valuation line should look like this:
valuation = doc.getElementById("eppraisalval").innerText

excel vba copying fedex tracking information

I want to copy into Excel 3 tracking information tables that website generates when I track a parcel. I want to do it through Excel VBA. I can write a loop and generate this webpage for various tracking numbers. But I am having a hard time copying tables - the top table, travel history and shipments track table. Any solution? In my vba code last 3 lines below are giving an error :( - run time error '438' Object doesn't support this property or error.
Sub final()
Application.ScreenUpdating = False
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://www.fedex.com/fedextrack/index.html?tracknumbers=713418602663&cntry_code=us"
With ie
.Visible = True
.navigate my_url
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
ie.document.getElementById("detailsBody").Value
ie.document.getElementById("trackLayout").Value
ie.document.getElementById("detail").Value
End Sub
.Value is not a method available in that context. also, you will want to assign the return value of the method call to a variable. Also, you should declare your variables :)
I made some modifications and include one possible way of getting data from one of the tables. YOu may need to reformat the output using TextToColumns or similar, since it prints each row in a single cell.
I also notice that when I execute this, the tables have sometimes not finished loading and the result will be an error unless you put in a suitable Wait or use some other method to determine when the data has fully loaded on the webpage. I use a simple Application.Wait
Option Explicit
Sub final()
Dim ie As Object
Dim my_url As String
Dim travelHistory As Object
Dim history As Variant
Dim h As Variant
Dim i As Long
Application.ScreenUpdating = False
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://www.fedex.com/fedextrack/index.html?tracknumbers=713418602663&cntry_code=us"
With ie
.Visible = True
.navigate my_url
'## I modified this logice a little bit:
Do While .Busy And .readyState <> 4
DoEvents
Loop
End With
'## Here is a simple method wait for IE to finish, you may need a more robust solution
' For assistance with that, please ask a NEW question.
Application.Wait Now() + TimeValue("0:00:10")
'## Get one of the tables
Set travelHistory = ie.Document.GetElementByID("travel-history")
'## Split teh table to an array
history = Split(travelHistory.innerText, vbLf)
'## Iterate the array and write each row to the worksheet
For Each h In history
Range("A1").Offset(i).Value = h
i = i + 1
Next
ie.Quit
Set ie = Nothing
End Sub

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