Excel VBA: cannot Get inner text of HTML id - html

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

Related

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

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

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.

HTTPS certificate error excel VBA

Tried to look into the old questions, but couldn't find the answer I was looking for.
I'm trying to make a spreadsheet that automatically scrapes the toner levels in our printers - however, there is a certificate error.
Instead of posting the website, IE (and therefore Excel/VBA) shows the cert.error HTML.
How can I bypass this? It is not possible to add the site to trusted sites - can VBA somehow ignore the error, or can I make VBA press tab, enter?
I've tried to use the following code, however no luck.
Enum readystate
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub ImportPrinterData()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "https://to-port3-hp1c/"
Do While ie.readystate <> READYSTATE_COMPLETE
Application.StatusBar = "Venter på printer data ..."
DoEvents
Loop
Set html = ie.document
Application.SendKeys "{tab 7}", True
Application.SendKeys "{return}", True
MsgBox html.DocumentElement.innerHTML
Set ie = Nothing
Application.StatusBar = ""
End Sub

VBA Scraping with IE11 - HTML Elements not indexing in VBA Local - Call javascript instead?

I've been scraping away for the last couple months with no issues, but now I have a site I'd like to access that is a bit more complicated than simple HTML. When I inspect elements, I can find the tag, ID, and name of an input box i'd like to fill, but when I run the code, there are no elements within the VBA locals window for my ie object.
I believe this is due to AJAX, but i'm too novice with ajax/javascript to really know for sure.
This is a screenshot of the input box i'd like to fill out:
and this is the code I'm using:
Sub Findie()
Dim ie As Object
Dim userName As Variant
Dim passWord As Variant
Dim strHTML As String
Dim sht As Worksheet
Dim itemRng As Range
Dim testObj As Object
Set sht = Sheets("Sheet1")
Set itemRng = sht.Range("A2")
userName = "***#***.com"
passWord = "********"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
strHTML = "http://[MYWEBSITE].com"
ie.Navigate strHTML
'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Application.Wait (Now + #12:00:03 AM#)
ie.Document.getElementsByName("username")(0).Value = userName
ie.Document.getElementsByName("password")(0).Value = passWord
ie.Document.getElementsByClassName("submit")(0).Click
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Application.Wait (Now + #12:00:01 AM#)
ie.Navigate "[MYWEBSITE]" 'after logging in, navigating to this page saves time
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
ie.Document.getElementsByName("productId")(0).Value = "12345"
This is not an issue with the page loading either, it does not work when I step through.
I'm thinking it will be much easier if I can actually run the scripts that are on the server, but I've never done that using VBA. I'm not really sure how. Here is a screenshot of the scripts available on the server. I don't really know where to go from here, or which method is ideal.

VBA HTML not running on all computers

So I have this code I'm working on for my deptarment that goes to a website inputs data , clicks run and downloads the csv file to the worksheet. It works just fine on my PC and on my profile on the computers the other department uses. We are both using same versions of windows, excel , and IE. When i have someone from the other department run the macro it opens the website but never enters the data into the fields despite the site being the exact same coding as when i'm logged in.
Sub testmetric()
Dim appIE As Object
Dim Obj As Object
On Error Resume Next
Sheets("Audit").Select
Selection.AutoFilter
Sheets("Audit").Cells.Clear
Application.ScreenUpdating = False
Application.ScreenUpdating = True
Set appIE = CreateObject("InternetExplorer.Application")
sURL = "http://cctools/rportal/main.php?p=agentavaya"
' Instructes the macro to open IE and navigate to sURL.
With appIE
.Navigate sURL
.Visible = True
Application.Wait Now + TimeValue("00:00:02")
Set HTMLDOC = .Document
End With
Application.Wait Now + TimeValue("00:00:02")
appIE.Document.getElementById("agentLob").selectedIndex = "3"
appIE.Document.getElementById("timezone").selectedIndex = "1"
appIE.Document.getElementById("sdate").Value = Range("Date_Start")
appIE.Document.getElementById("edate").Value = Range("Date_End")
appIE.Document.getElementById("stenure").Value = Range("TenStart")
appIE.Document.getElementById("etenure").Value = Range("TenEnd")
Application.Wait Now + TimeValue("00:00:02")
For Each Btninput In appIE.Document.getElementsByTagName("INPUT")
If Btninput.Value = " Run " Then
Btninput.Click
Exit For
End If
Next
Application.Wait Now + TimeValue("00:00:02")
Call CSV
appIE.Quit
Sheets("Audit").Select
Range("A1").Select
Sheets("audit").Paste
End Sub
Sub CSV()
sCSVLink = "http://cctools/rportal/csvexport.php"
sfile = "csvexport.php"
ssheet = "Sheet10"
Set wnd = ActiveWindow
Application.ScreenUpdating = False
Workbooks.Open Filename:=sCSVLink
Windows(sfile).Activate
ActiveSheet.Cells.Copy
wnd.Activate
Range("A1").Select
Sheets("Audit").Paste
Application.DisplayAlerts = False
Windows(sfile).Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Sheets("Audit").Select
Range("A1").Select
Sheets("audit").Paste
End Sub
When this code is ran by a member of the other department it just opens the website inputs nothing and doesn't press the RUN button on the website.
Any ideas on what could be causing this? What setting or anything. I verified that both PC's VBA references in are there and no "Locations are missing paths" .
You are never checking to see if the web page has finished loading!
Your On Error Resume Next statement is also hiding any errors and preventing you from taking the appropriate remedial action by fixing the code. Please report back with the specific error number and line which raises the error. I have an idea which line and which error, and my answer is intended for that:
Type 91 error on line: Set HTMLDOC = .Document
Why?
While you are waiting, Application.Wait is a really ineffective method to wait for the browser to load, and 2 seconds may not always be enough time anyways. The best way I can think of to handle this, assuming you're getting a type 91 error, is like so:
How to make a ready-waiting loop for Browser control
Basically you need to put the thread in a loop until the browser has loaded the page. In pseudo-code, you're doing:
Do Until Browser Is Loaded
DoEvents 'etc.
Loop
Using the WinAPI Sleep Function
You will need to use the WinAPI Sleep function (a lot of people use DoEvents or Application.Wait but I have found here that Sleep is preferable.
Because it is a Declare, you must put it in an ordinary module, it can't go in a class or userform module.
'## This needs to go in an ordinary code module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Calling the WinAPI Sleep Function
Sleep 250 'sleep for 1/4 of a second, or Sleep 1000 to sleep for a full second, etc.
Putting it all together
You will need to put a Do ... Loop that calls on this function immediately after the .Navigate method.
'## This goes in where you do Navigate method:
With appIE
.Navigate sURL
Do
Sleep 250
Loop While Not .ReadyState <> 4 'READYSTATE_COMPLETE
Set HTMLDoc = .Document
End With
If it still doesn't work...
Yes, there's always an exception. I do very little web-based automation, and most of what I do is just tinkering with stuff here to help people out. What I have noticed is that increasingly websites are served dynamically (AJAX maybe?) and that in these cases, the browser may be .ReadyState = READYSTATE_COMPLETE and even .Busy = False, yet the .Document is still Nothing.
IF that happens, you could put a secondary loop, like:
Dim numberOfAttempts
Do While .Document Is Nothing
Sleep 250
numberOfAttempts = numberOfAttempts + 1
If numberOfAttempts > 100 Then
MsgBox "This is taking too long, try again later."
Exit Do
End If
Loop
You may want to add an iterator/counter as I have done there and simulate a timeout otherwise this could potentially go in to an infinite loop.
Thanks for all the great tips going to implement plenty of them. The true error behind this was a Windows settings. UAC :
http://windows.microsoft.com/en-us/windows7/products/features/user-account-control
Setting it down to the bottom "never notify" fixed it immediately.