How to copy website data and paste it to excel worksheet? - html

I have a very simple goal but have had some struggles truly implementing it.
Essentially I want to run a vba code that will
1.) Open a webpage in IE
2.) Copy the entire webpage (Basically click Control A on the page)
3.) Copy that information onto a worksheet
Right now the code just opens the website put I cannot get it to copy the page and then copy it to a worksheet. Thank you for your expertise.
Sub website_test()
'Download Projections as CSV
'Download Projections as
Dim ie As Object
Dim ht As HTMLDocument
Dim Button As Object
Dim i As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("https://www.numberfire.com/nba/daily-fantasy/daily-basketball-projections")
Do Until ie.readyState = 4
DoEvents
Loop
Set ht = ie.document
Set elems = ht.getElementsByClassName("title may-blank")
For Each elem In elems
Debug.Print (elem.innerText)
Next
Set Button = ht.querySelector("span.hov-underline")
'Button.Click
app.document.execCommand "SelectAll", False
app.document.execCommand "Copy", False
'wait the download prompt appear
Application.Wait (Now + TimeValue("00:00:03"))
Application.SendKeys "%{s}"
'Waiting for the site to load.
Application.Wait (Now + TimeValue("00:00:03"))
ie.Quit
Range("A1").Select
ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=
_False, NoHTMLFormatting:=True
ActiveSheet.Cells.WrapText = False
ActiveSheet.Cells.MergeCells = False
'ie.Quit
End Sub

Related

Open a webpage with VBA and click on the "active directory" button to log in

I want to open a web page directly from Excel VBA where I need to click on the "active directory" button which has no ID. This button uses the Windows credentials to automatically log so I just need to click there to later once I'm logged I can start populating some fields.
This generic code is giving me an error message
" The object invoked has disconnected from its clients"
I assume it's related to the web page secure log on.
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub
IE is EOL and shouldn't be used anymore.
I can't tell you how exactly on the page the button is clicked, because that requires the HTML code. But the disconnection from the client may be due to the way IE is initialized. Try it via the GUID D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub

Export button cannot be clicked on HTML page with VBA

I'm automating an HR webpage (where you have to login to see info, so I can't share access), however, after getting the query I want, I can't click a specific button. Code for that button is:
<div class="fbutton">
<div>
<span class="export_xls" style="padding-left: 20px;">Exportar XLS</span>
Class Name is "export_xls"
OutterText, InnerText and InnerHTML is the same: "Exportar XLS"
I have tried many things, like going into loop on every "div" or "span" object and match one of the elements I mentioned earlier and click it if found, no one is even recognizing the "Exportar XLS" text.
My code is the following:
Sub extraer_horarios()
Dim MyHTML_Element As IHTMLElement
Dim URL As String
Dim IE As Object
Dim Element As HTMLLinkElement
Dim IeDoc As Object, post As Object, elem As Object
Set IE = CreateObject("InternetExplorer.Application")
URL = "http://201.122.64.119:40/accounts/login/?next=/data/worktable/"
IE.Visible = True
IE.navigate URL
WaitFor IE
IE.Document.all.Item("id_username").innerText = "*****"
IE.Document.all.Item("id_password").innerText = "*****"
IE.Document.all.Item("id_login").Click
Application.Wait (Now + TimeValue("00:00:05"))
Set IE = GetIE("http://201.122.64.119:40/data/worktable/")
IE.navigate "http://201.122.64.119:40/page/att/AttReport/"
Application.Wait (Now + TimeValue("00:00:05"))
IE.Document.all.Item("LEReport").Click
IE.Document.all.Item("id_cometime").Value = "2019-08-01"
IE.Document.all.Item("id_endtime").Value = "2019-08-31"
IE.Document.all.Item("id_drop_emp").Click
IE.Document.GetElementbyID("id_per_count").Focus
AppActivate "BioTime"
Application.SendKeys "{END}"
Application.Wait (Now + TimeValue("00:00:05"))
Dim cBox As Object
Set cBox = IE.Document.getElementsByClassName("chk_selected_all")(0)
cBox.Click
IE.Document.all.Item("id_close").Click
IE.Document.all.Item("id_query").Click 'Until here everrything works fine
'Here the problem starts
Set HTMLDoc = IE.Document
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("*")
If MyHTML_Element.innerText = "Exportar XLS" Then MyHTML_Element.Click: Exit For
Next
End Sub
If you want any other part of the code or something I can facilitate other than the impossiblity of sharing the access to webpage, please ask me so. Thank you for your help.

How to copy/paste data from excel to a webpage (VBA)?

So I'm trying to copy/paste data from excel into a webpage text box using VBA. However, my problem is that if I have, for example 3 or 4 rows of data copied, when pasting the values into the webpage using vba, only 1 row will be copied rather than all the rows.
Here is my code:
.Document.getElementsByTagName("textarea")(0).Value = ActiveCell.Value
Any ideas? If I take out the (0) I get an error:
object doesn't support this property or method.
This is some code of mine that works:
Sub FillOutInvoice(Account As Account)
Dim ie As InternetExplorer
Dim elem As HTMLLinkElement
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate Settings.ErpAddress
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
With ie.document
.getElementsByClassName(Settings.InputClass)(0).innerText = Account.InvoiceNumber
.getElementsByClassName(Settings.InputClass)(1).innerText = Day(Account.InvoiceDate)
End With
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Application.Wait Now + #12:00:02 AM#
ie.Quit
End Sub
As you see, the needed property is .InnerText and not .Value.
Here is an example of sending a range of rows text to a textarea element using the clipboard
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub InsertData()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.google.com/search?q=google+translate&rlz=1C1GCEB_enGB815GB815&oq=google+tran&aqs=chrome.0.0j69i57j0l4.2057j0j7&sourceid=chrome&ie=UTF-8"
While .Busy Or .readyState < 4: DoEvents: Wend
Dim clipboard As Object
Set clipboard = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
ActiveSheet.Range("A1:A3").Copy
With clipboard
.GetFromClipboard
ie.document.getElementsByTagName("textarea")(1).innerText = .GetText
End With
Application.CutCopyMode = False
Stop
.Quit
End With
End Sub
Contents of Range("A1:A3")

Excel VBA code to click web button

Need help how to create excel vba code for this
I'll be needing the codes so I can complete my macro.
Thanks in advance
First, you will need to create a reference to:
Microsoft Internet Controls
Microsoft HTML Object Library
In VBE, click Tools > References
Sub clickLink()
Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
Url = "http://UrlToYourLink.com"
With ie
.navigate Url
Do While .Busy Or .readyState < READYSTATE_COMPLETE
DoEvents
Loop
doc = .document
.Visible = True
End With
Dim myBtn As Object
Set myBtn = doc.getElementsByClassName("button rounded")(0)
myBtn.Click
End Sub
The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
Microsoft internet controls are a great way to do this, but if you aren't allowed to add new references, here is another way to go about web scraping.
This methode ain't as 'clean' as Microsoft internet controls and HTML object but it gets the job done.
Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer
'initialize counter
i = 0
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'navigate to the url
ie.navigate "Www.google.com"
'Statusbar shows in the buttom corner of excel
Application.StatusBar = "Loading, please wait..."
'Wait until page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Store all the elements with input tag
Set objCollection = ie.Document.getElementsByTagName("input")
'Go through all input elements
While i < objCollection.Length
'input search field
If objCollection(i).Name = "q" Then
objCollection(i).Value = "Hello World"
End If
'search button
If objCollection(i).Type = "submit" Then
Set objSearchBnt = objCollection(i)
End If
i = i + 1
Wend
objSearchBnt.Click
'Clean up
Set objSearchBnt = Nothing
Set objCollection = Nothing
Set ie = Nothing
'Give excel control over the status bar agian
Application.StatusBar = ""
End Sub

Excel VBA: cannot Get inner text of HTML id

Hi: I'm using excel to get values from this webpage: https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html
How can I get the code for excel VBA to get the fiels for NIF, EJF, MOD and CEL?
I tried with getelementbyid("NIF") and "name" but with no results
Thanks!
Previos thread: Excel VBA: Get inner text of HTML table td
This is the code I use:
Sub AEAT()
Dim IE As Object
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html"
Application.Wait (Now + TimeValue("0:00:02"))
IE.Document.getElementById("NIF").Value = Range("A1").Value
Application.Wait (Now + TimeValue("0:00:01"))
IE.Document.getElementById("EJF").Value = "2016"
Application.Wait (Now + TimeValue("0:00:01"))
IE.Document.getElementById("MOD").Value = "347"
Application.Wait (Now + TimeValue("0:00:01"))
IE.Document.getElementById("CEL").Value = Range("a4").Value
Application.Wait (Now + TimeValue("0:00:01"))
IE.Document.getElementById("env_button").Click
End Sub
Okie, so I don't have a Excel in Windows VM, so I tested your code using VBScript. So the solution should work. You have 3 problems
Navigate doesn't wait for Page for to load
Your site installs a ActiveX control, which needs to be loaded first
Even when you fix #1 and #2, you will get an exception "The client disconnected" and the object will not be usable anymore
Solution
Add the site to Trusted Sites in your IE Setting.
Open IE manually and install the ActiveX control
Sub AEAT()
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.visible = True
hwnd = ie.hwnd
IE.Navigate "https://www2.agenciatributaria.gob.es/es13/h/iexmmmfi.html"
msgbox ("wait")
Set oShell = CreateObject("Shell.Application")
For Each Wnd In oShell.Windows
If hwnd = Wnd.hwnd Then Set ie = Wnd
Next
IE.Document.getElementById("NIF").Value = "123"
End Sub
Call AEAT
I have put msgbox to insert wait for now, but you can fix that yourself by using below in excel
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
PS: reference from below URLs
Internet Explorer VBA Automation Error: The object Invoked has disconnected from its clients
Error "The object invoked has disconnected from its clients" - automate IE 8 with python and win32com