Extract html source code into excel using VBA - excel

I am trying to simply paste the content or innertext into excel using getElementByID function.
The content is actually the iframe link which I am trying to extract it and paste into cell.
The photo shown is the html source code.
Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim result As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set htmldoc = ie.document
Set Results = HTML.getElementById("bm_ann_detail_iframe")
Sheets("Sheet1").Range("a1").Value = Results.innerText
End Sub
html source code

You should use consistent variable naming in your code. If you put Option Explicit at the top of your code that will help.
You want to access the src attribute of the iframe to get the URL shown.
If you plan to use the new URL then you actually want the part before the "#". This means changing to:
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Split(ie.document.getElementById("bm_ann_detail_iframe").src, "#")(0)
Code:
Option Explicit
Public Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
ThisWorkbook.Worksheets("Sheet1") = ie.document.getElementById("bm_ann_detail_iframe").src
ie.Quit
End Sub

Related

Click on Class names in VBA - No Selenium

I am into Project Management with no knowledge of IT / Coding.
I am trying to do web automation.
It will have to click on certain links, submit a form and then hit save.
However I am not able to click on the first link as well.
this is my code as of now.
Sub CommandButton1_Click()
Dim ie As Object
Dim html As HTMLDocument
Dim form As Variant, button As Variant
Sheet1.Range("B6").Value = Application.UserName
'Open Internet Explorer
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://www.link.com/"
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
html.getElementsByClassID("p2205").Click
End Sub
Below is the code.
I want to click on Company Name.
Website Code
I have tried the web and YouTube videos as well but I guess I don't know the language or the logic behind coding. Hence I am not able to get through.
Add reference to Microsoft Internet Controls then try
Option Explicit
Sub CommandButton1_Click()
Dim ie As Object, html As HTMLDocument, e As HTMLHtmlElement
Dim URL As String
URL = "https://www.link.com/"
Set ie = New InternetExplorerMedium
ie.navigate URL
ie.Visible = True
While ie.Busy
DoEvents
Wend
Set html = ie.document
For Each e In html.getElementsByClassName("dsh_sta_301")
If InStr(e.onclick, "projectSelection") > 0 Then
e.Click
End If
Next
End Sub

Excel VB Macro to scrape webpage. Can't code to click html button

I have a short excel macro that is designed to:
1) Open Internet Explorer and navigate to "http://www.puco.ohio.gov/pucogis/address/search.cfm"
2) Fill out a form on that site with data from the excel workbook
3) Click a button to submit the form
4) Scrape some innertext from the website and place it in a cell in the workbook
5) Close Internet Explorer
I can not get step 3 to work. That is, I can not get the click/submit function to work with this website. When the button is clicked the website populates with information specific to the information entered in the form. Everything else in the code is working. I have searched for an answer and tried the submit verses click approach with no luck.
Thanks for you help.
Code below:
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Set HTMLButton = ie.Document.getElementsByTagName("input")(1)
HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
Try this solution, which I found from this answer to a similar question. That answer was not accepted, but I have tested this with your code and seems to be working.
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Call ie.Document.parentWindow.execScript("codeAddress()")
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
If you don't know or can't reasonably anticipate the function call codeAddress(), then you can try something like this to derive it from the button's onclick property:
Dim fn$
fn = HTMLButton.onclick
fn = Mid(fn, InStr(fn, "{"))
fn = Trim(Replace(Replace(Replace(fn, "{", vbNullString), "}", vbNullString), vbLf, vbNullString))
Call ie.Document.parentWindow.execScript(fn)
You can call the JavaScript directly. try this it will work
Instead of:
Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
HTMLButton.Click
use
ie.Document.parentWindow.execScript code:="codeAddress()"
note that IE may prompt you to confirm every run so you may need to
stop showing this message for smooth operation
Private Sub CommandButton1_Click()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
ie.Document.parentWindow.execScript code:="codeAddress()"
'Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
'HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D16") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
thanks also to this article helped me to solve your problem
How to find and call javascript method from vba

VBA Scraping Data From Multiple Websites

I am trying to use VBA to scrape commodity/stock prices from multiple pages on investing.com and insert them into an excel spreadsheet.
The following code is what I have working to do a single price, in this example gold:
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.navigate "http://uk.investing.com/commodities/gold"
Do
DoEvents
Loop Until ie.READYSTATE = READYSTATE_COMPLETE
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Quit
End Sub
However I need data from multiple sites to get different prices, all at the same time.
I tried elaborating on the code I have that is working, the following example is what I tried to display the price of gold and silver, however it only displayed the gold price in cells A1 & A2:
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.navigate "http://uk.investing.com/commodities/gold"
Do
DoEvents
Loop Until ie.READYSTATE = READYSTATE_COMPLETE
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Quit
ie.navigate "http://uk.investing.com/commodities/silver"
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A2").Value = output
ie.Quit
End Sub
Please could somebody help me figure out how to get this to work for multiple pages? I have tried searching however have not come up with anything that suits my needs.
Also is it possible to get something to pop up saying something like "Waiting...." whilst the data is being collected?
Thanks
I've found that using READYSTATE is not reliable in the sense that the document hasn't fully loaded sometimes - or at least the object model hasn't loaded.
So I typically add a sleep command and Doevents before trying to access the new doc object
This should work for you (and as #Dave says, you don't need to use IE.Quit)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.Navigate "http://uk.investing.com/commodities/gold"
Do
Sleep 500
DoEvents
Loop Until ie.ReadyState = 4 ' READYSTATE_COMPLETE
Sleep 500
Set doc = ie.Document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Navigate "http://uk.investing.com/commodities/silver"
Do
Sleep 500
DoEvents
Loop Until ie.ReadyState = 4 ' READYSTATE_COMPLETE
Sleep 500
Set doc = ie.Document
output = doc.GetElementById("last_last").innerText
Range("A2").Value = output
ie.Quit
Set ie = Nothing
End Sub

VBA Excel Click on a Label

I am new to Excel VBA. I am currently doing excel IE Automation where I want to click label on webpage.
The HTML source code is:
<td class = t19TabItem>Compliance</td>
Any help to click on label compliance.
I used the below code, has found the label compliance by using inner html, but it does not click on compliance tab. It throws errors as object does not this property.
set link=IE.document.getElementsByTagName("a")
if link.InnerHTML="compliance" and link.href="javascript:apex.submit('D_Price')" then
click link
end if
How to click on compliance label?
try the code with references below
Microsoft HTML Object Library
Microsoft Internet Controls**
Sub test()
Dim oHTML_Element As IHTMLElement
Dim oBrowser As InternetExplorer
Dim ie As Variant
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "your web link" 'Your weblink goes here
While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
DoEvents
Wend
Application.Wait (Now() + TimeValue("00:00:03"))
For Each oHTML_Element In ie.document.getElementsByTagName("a")
If oHTML_Element.innerHTML = "compliance" Then
oHTML_Element.Click
End If
Next
End Sub

Excel VBA object required error with IE automation

So I have some code to go to a website and then login. Then I need it to go to the password change URL. Sometimes when I run the code, I get the following error:
Runtime Error 424: Object Required
Sometimes the debugger says it is the first getelementbyid statement, but most times it says the issue is the last three getelementbyid statements. The code is below:
Dim ie As Variant
Dim strURL As String
Sub login()
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "https://minecraft.net/profile"
While ie.Busy
DoEvents
Wend
ie.document.getElementById("username").Value = "ddd"
ie.document.getElementById("password").Value = "ddddddddddd"
Dim htmlForm As HTMLFormElement
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
' **********************************************************************
'IE.Document.getElementById("username").Value = "ddddd"
' IE.Document.getElementById("password").Value = "ddddd"
' IE.Document.getElementById("signin").Click
'**********************************************************************
'Pause while page loads
Application.Wait (Now + #12:00:03 AM#)
ie.navigate "https://minecraft.net/profile/password"
ie.document.getElementById("oldPassword").Value = "oldpass"
ie.document.getElementById("password").Value = "enwapss"
ie.document.getElementById("passwordConfirmation").Value = "enwapss"
Set htmlForm = ie.document.getElementById("loginForm")
htmlForm.submit
End Sub
Thanks in advance!
It may be that the website isn't in a ready state, as in the site hasn't fully loaded when it's attempting to input the values.
After
ie.navigate "https://minecraft.net/profile/password"
Try adding
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
This will loop until the webpage has loaded similar, to the way you've done it in your above code with
Application.Wait (Now + #12:00:03 AM#)

Resources