Read Data From Web Form to Excel using VBA? - excel

I am new to VBA and I need to fetch values from this form into an Excel spreadsheet. Once the user clicks on estimate, the exchange rate is displayed. The Excel spreadsheet will have column headings matching the field names in the form.
In the form, The 'Send Amount' will be 50, 100, 500, 1000, 2500 and 7000. The 'From' Country will always be 'United States'. The worksheet should be populated with all the possible selections in the other fields of the form against 'United States' and their corresponding exchange rates.

Santosh...There are two ways of doing this
Intercept the webservice XML
Create connection to underlying DB
You need to know DB details or create COM modules in VBA for XML (which again needs server details)
if you do not know anything at all. I would suggest go to youtube and look for "VBA com connections" or "VBA XML". There are some nice videos, handing over server details on a open forum would not be an ideal thing to do

No luck here, site writes "Connecting..." eternally.But to sort out your issue, here's a sample code:Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myIE As InternetExplorer, myDoc As HTMLDocument, mySubDoc As HTMLDivElement
Dim sVal As String, lVal As Long
Set myIE = New InternetExplorer
With myIE
.Visible = True
Call .navigate("http://stackoverflow.com/")
While Not .readyState = READYSTATE_COMPLETE
DoEvents
Wend
Set myDoc = .document
End With
'SAMPLE
With mySubDoc
Set mySubDoc = myDoc.getElementsByClassName("question-summary narrow").Item(0)
sVal = CStr(mySubDoc.ID)
lVal = CLng(mySubDoc.NodeType)
Call MsgBox(sVal)
Call MsgBox(CStr(lVal))
End With
On Error Resume Next
Call mySubDoc.Close
Call myDoc.Close
Call myIE.stop
Call myIE.Quit
On Error GoTo 0
Set mySubDoc = Nothing
Set myDoc = Nothing
Set myIE = Nothing
End SubPlease note: In Excel's VBA (ALT+F11) you have to add Reference (Tools->References) to Microsoft HTML Object Library and Microsoft Internet Controls items.You can use the above guy to pick up values from the page source of the loaded page on your target site. So you have to set values you want, press the "Estimate" button, 'View Page Source' and look for the values' container node which you want to extract from VBA code (and add to your Excel VBA of course).With having your site down -ie inaccessible result page- that's all I can do but with the above code you can figure out the rest of what you have to do in Excel VBA for sure.If not, post a new question or ask here when site is up again.Hope this helps!

Related

Using Excel VBA to automate form filling in Internet Explorer

I want to take values from an excel sheet and store them in an array. I then want to take the values from the array and use them to fill the web form.
I have managed to store the values in the array and I have managed to get VBA to open Internet Explorer (IE)
The code runs and no errors appear, but the text fields are not being populated, nor is the button being clicked
(The debugger points to [While .Busy] as the error source, located in the WITH block)
How do I go about filling the form (that has a total of 3 text boxes to fill)?
There is also a drop down menu that I need to choose a value from, but I need to fill the text boxes prior to moving on to that part of the task.
Sub CONNECT_TO_IE()
the_start:
Dim ie As Object
Dim objElement As Object
Dim objCollection As Object
acct = GET_CLIENT_NAME()
name = GET_CODE()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("<<my_website>>")
ie.FullScreen = False
On Error Resume Next
Do
DoEvents
If Err.Number <> 0 Then
ie.Quit
Set ie = Nothing
GoTo the_start:
End If
Loop Until ie.readystate = 4
Application.Wait Now + TimeValue("00:00:10")
ie.Document.getElementbyid("<<field_1>>").Value = "PPP"
ie.Document.getElementbyid("<<field_2>>").Value = "PPP"
ie.Document.getElementbyid("<<field_3>>").Click
Set ie = Nothing
End Sub
UPDATE: Turns out the reason this wasn't working is because there are some settings in the HTML of the site that do not allow for the automation to occur, so any code versions I had were correct but they were doomed to fail. So you were correct in that regard #TimWilliams.
I know this because the website I was trying to access is on a secure server/virtual machine. I edited the code to fill in the google search bar and it did not work on the virtual machine however when I ran the same code locally, it worked fine.

Copy specific image from webpage and paste into Excel using VBA

I'm trying to take a character string from a cell, copy it into a free QR code generator (ex| http://goqr.me/ ), and bring the resulting image back into my spreadsheet. (I like this generator because you don't need to click "submit" - it updates the image as the text is entered. Assuming/hoping that feature makes this easier)
I'm trying to avoid the installation of new barcode fonts and the purchasing of barcode specific packages.
Using the below code, I can get the browser to open and enter the desired text. I need a way to get the image back into my spreadsheet now. The code I'm using currently just returns "[object]" (as text) in cell C1.
How can I use VBA to get this image onto my clipboard?
Public ieApp As Object
Sub Barcode()
Dim ieDoc As Object
Dim InputBox As Object
Dim qrBoxImage as Object
barcodeTerm = Range("B1").Value
If TypeName(ieApp) = "Object" Or TypeName(ieApp) = "Nothing" Then
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Navigate ("http://goqr.me/")
End If
While ieApp.ReadyState <> 4
DoEvents
Wend
ieApp.Visible = True
Set ieDoc = ieApp.Document
Set InputBox = ieDoc.getElementsByName("text")
Set qrBoxImage = ieDoc.getElementByID("qrcode-preview-image")
InputBox.Item(0).Value = barcodeTerm
Range("C1").Value = qrBoxImage
Set ieDoc = Nothing
End Sub
You can follow the steps detailed over here:
http://social.technet.microsoft.com/wiki/contents/articles/23860.print-screen-to-an-image-using-access-vba.aspx
Although the example is used in Access, it's still VBA code so it still should work in Excel

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

How do I keep the new structure of web imported data after refresh?

I am creating an Excel database. I would like to import names, emails and job positions of all employees of a firm from the firm website.
I choose Data->From Web and select the whole page, as it is the only possibility.
The page shows no table with data; just a long list of photos of employees with names, emails and job positions next to them
I import the data into my Excel spreadsheet: the format is very bad. So I begin cut and paste creating a column for "names", one for "email" and similarly for "job position". All other information is manually canceled.
I would like to refresh data keeping this new format. Unfortunately, every time I refresh the imported data using the "refresh all" button, they return to the original format.
How can I keep the new format of my web imported data, after refresh?
I thank you all for your support!
Kr,
A
I've put together an example that will extract the name and title from that page you specified and put them into sheet 1.
The code will only work providing the layout of the underlying html remains the same. It does not support updating of an existing list (anything on sheet 1 is removed prior to reading the list again)
To use this code you must place it in a new code module (not the worksheet or workbook sections) and you can run it either from the code editor or via the macros menu in the main excel window.
' Note: This code requires the following references to be loaded.
' Microsoft HTML Object Library (mshtml.tlb)
' Microsoft Internet Controls (ieframe.dll)
' To add a reference
' In the VBA Code Editor, in the Tools Menu click the References item
' Scroll through the list and ensure that the references are selected
' Press OK and your done.
Sub Scrape()
Dim Browser As InternetExplorer
Dim Document As HTMLDocument
Dim Element As IHTMLElement
Dim Elements As IHTMLElementCollection
Dim empName As String
Dim empTitle As String
Dim Sheet As Worksheet
Set Sheet = ThisWorkbook.ActiveSheet
Sheet.UsedRange.ClearContents ' Nuke the old list
Set Browser = New InternetExplorer
Browser.navigate "http://www.hsbc.com/about-hsbc/leadership"
Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Document = Browser.Document
Set Elements = Document.getElementsByClassName("profile-col1")
For Each Element In Elements
empName = Trim(Element.Children(1).Children(0).innerText)
empTitle = Trim(Element.Children(1).Children(1).innerText)
Sheet.Range("A1:B1").Insert xlShiftDown
Sheet.Cells(1, 1).Value = empName
Sheet.Cells(1, 2).Value = empTitle
'Debug.Print "[ name] " & empName
'Debug.Print "[ title] " & empTitle
Next Element
Set Browser = Nothing
Set Elements = Nothing
End Sub

How does one wait for an Internet Explorer 9 frame to load using VBA Excel?

There are many online resources that illustrate using Microsoft Internet Explorer Controls within VBA Excel to perform basic IE automation tasks. These work when the webpage has a basic construct. However, when webpages contain multiple frames they can be difficult to work with.
I need to determine if an individual frame within a webpage has completely loaded. For example, this VBA Excel code opens IE, loads a webpage, loops thru an Excel sheet placing data into the webpage fields, executes search, and then returns the IE results data to Excel (my apologies for omitting the site address).
The target webpage contains two frames:
1) The searchbar.asp frame for search value input and executing search
2) The searchresults.asp frame for displaying search results
In this construct the search bar is static, while the search results change according to input criteria. Because the webpage is built in this manner, the IEApp.ReadyState and IEApp.Busy cannot be used to determine IEfr1 frame load completion, as these properties do not change after the initial search.asp load. Therefore, I use a large static wait time to avoid runtime errors as internet traffic fluctuates. This code does work, but is slow. Note the 10 second wait after the cmdGO statement. I would like to improve the performance by adding solid logic to determine the frame load progress.
How do I determine if an autonomous frame has finished loading?
' NOTE: you must add a VBA project reference to "Internet Explorer Controls"
' in order for this code to work
Dim IEapp As Object
Dim IEfr0 As Object
Dim IEfr1 As Object
' Set new IE instance
Set IEapp = New InternetExplorer
' With IE object
With IEapp
' Make visible on desktop
.Visible = True
' Load target webpage
.Navigate "http://www.MyTargetWebpage.com/search.asp"
' Loop until IE finishes loading
While .ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
End With
' Set the searchbar.asp frame0
Set IEfr0 = IEapp.Document.frames(0).Document
' For each row in my worksheet
For i = 1 To 9999
' Input search values into IEfr0 (frame0)
IEfr0.getElementById("SearchVal1").Value = Cells(i, 5)
IEfr0.getElementById("SearchVal2").Value = Cells(i, 6)
' Execute search
IEfr0.all("cmdGo").Click
' Wait a fixed 10sec
Application.Wait (Now() + TimeValue("00:00:10"))
' Set the searchresults.asp frame1
Set IEfr1 = IEapp.Document.frames(1).Document
' Retrieve webpage results data
Cells(i, 7) = Trim(IEfr1.all.Item(26).innerText)
Cells(i, 8) = Trim(IEfr1.all.Item(35).innerText)
Next
As #JimmyPena said. it's a lot easier to help if we can see the URL.
If we can't, hopefully this overview can put you in the right direction:
Wait for page to load (IEApp.ReadyState and IEApp.Busy)
Get the document object from the IE object. (done)
Loop until the document object is not nothing.
Get the frame object from the document object.
Loop until the frame object is not nothing.
Hope this helps!
I used loop option to check the field value until its populated like this
Do While IE.Document.getElementById("USERID").Value <> "test3"
IE.Document.getElementById("USERID").Value = "test3"
Loop
this is a Rrrreeally old thread, but I figured I would post my findings, because I came here looking for an answer...
Looking in the locals window, I could see that the "readystate" variable was only "READYSTATE_COMPLETE" for the IE App itself. but for the iframe, it was lowercase "complete"
So I explored this by using a debug.print loop on the .readystate of the frame I was working with.
Dim IE As Object
Dim doc As MSHTML.HTMLDocument
Set doc = IE.Document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = doc.Frames("TheFrameIwasWaitingFor").Document
' then, after I had filled in the form and fired the submit event,
Debug.Print iframeDoc.readyState
Do Until iframeDoc.readyState = "complete"
Debug.Print iframeDoc.readyState
DoEvents
Loop
So this will show you line after line of "loading" in the immediate window, eventually showing "complete" and ending the loop. it can be abridged to remove the debug.prints of course.
another thing:
debug.print iframeDoc.readystate ' is the same as...
debug.print doc.frames("TheFrameIwasWaitingFor").Document.readystate
' however, you cant use...
IE.Document.frames("TheFrameIwasWaitingFor").Document.readystate ' for some reason...
forgive me if all of this is common knowledge. I really only picked up VBA scripting a couple days ago...

Resources