I'm looking for a code to put values from an Excel to a Webpage.
Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://xxxxxxxx/"
IE.Visible = True
While IE.Busy
DoEvents
Wend
IE.document.getElementById("xxxxx").Value = "xxxx"
End Sub
The error comes in on IE.document.getElementById("xxxxx").Value = "xxxx line and it says Method 'Document' of object 'IWebBrowser 2' failed.
I'm looking for suggstions to solve this question: I made a lot of research and nothing works :/
Thanks in advance :)
Ana,
Here is a working example. You can easily customized this little bit of code to work for you. One time you must be aware of is that I added a reference to my project under TOOLS -> Preferences -> Microsoft Internet Controls. Also another word of caution is on this line Set Elements on this line, if you are looking for something that does not exists you will end up with an error. This working example I put together goes to stack overflow and finds the elements below footer-menu.
Let me know if something does not make sense.
Sub TryInternetExplorer()
'Reference to system32\shdocvw.dll 'Microsoft Internet Controls
Dim IEApp As InternetExplorer
Set IEApp = New InternetExplorer
IEApp.Visible = True
IEApp.Navigate "https://iam.pearson.com/auth/XUI/#login/"
While IEApp.ReadyState <> READYSTATE_COMPLETE And IEApp.ReadyState <> READYSTATE_LOADED
DoEvents
Wend
'wait for the form to load
Do
Set form = IEApp.Document.getElementsByTagName("FORM")(0) '1st table
DoEvents
Loop While form Is Nothing
IEApp.Document.getElementsByName("callback_0")(0).Value = "Miguel"
IEApp.Document.getElementsByName("callback_1")(0).Value = "pass"
IEApp.Document.getElementsByName("callback_2")(0).Click
Set Document = Nothing
Set IEApp = Nothing
End Sub
Related
I am new at vba coding to pull data from website so generally, I use this code to connect and check item to pull data from website but this code cannot check data via watch in vba with my firm webapp. it show nothing when I add watch to the class so what should I do.HTML Code from my firm webapp 1
HTML Code from my firm webapp 2
Sub Connect_web()
Dim ie As InternetExplorer
Dim doc As HTMLdocument
Dim ele As IHTMLElement
Dim col As IHTMLElementCollection
Dim ele_tmp As IHTMLElement
Set ie = New InternetExplorer
URL = "" ' Cannot provide
ie.Visible = True
ie.navigate URL
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading Page..."
DoEvents
End If
Loop
Set doc = ie.Document
Set ele = doc.getElementByClassName("GDB3EHGDHLC")
end sub
Let's start with four things:
1) Instead of .Navigate use .Navigate2
2) Use a proper wait
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
3) Correct the syntax of your Set ele line. You are using ByClassNamewhich returns a collection and therefore is plural. You are missing the s at the end of element.
As you have declared ele as singular (element), perhaps first set the collection into a separate variable and index into that collection.
Dim eles As Object, ele As Object
Set eles = doc.getElementsByClassName("GDB3EHGDHLC")
Set ele = eles(0)
4) You should always use id over other attributes, if possible, as id is usually quicker for retrieval. There is an id against that class name in your image (highlighted element). I am not going to try and type it all out. Please share your HTML using the snippet tool, by editing your question, so we can relate to your html in answer easily.
Set ele = doc.getElementById("gwt-debug-restOfIdStringGoesHere")
I am trying to use VBA to navigate IE to search some of the information for me. However, after the code is written and I run it. I found everything is fine before the part of clicking href, but if I use "F8" to execute the code one by one and it works. Unfortunately, I haven't found some situation like mine in this forum. Can someone give some hints to me? Thanks
Sub GetData()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
Dim HTMLAllhref As MSHTML.IHTMLElementCollection
studentid = 12345678
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate "https://xxx.xxx"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("abcd")
HTMLInput.Focus
HTMLInput.FireEvent("onchange")
HTMLInput.Value = studentid
Set HTMLButton = HTMLDoc.getElementById("submit")
HTMLButton.Click
The part above works properly, but the following part works only if I use "F8" to execute one by one
Set HTMLAllhref = HTMLDoc.GetElementsByTagName ("a")
For Each link in HTMLAllhref
If InStr(link.innerText, studentid) Then
IE.Navigate link.href
Exit For
End If
Next
End Sub
1) If works with F8 it is usually a timing issue and you need to determine where a wait needs to happen.
As a minimum be sure to use a proper wait after each .Navigate2, .Click and .Submit to allow for page loading:
While IE.Busy Or ie.readyState < 4: DoEvents: Wend
Potentially also after:
HTMLInput.FireEvent("onchange")
2) The recommended method is .navigate2, not .navigate.
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
Problem: Attempting to copy/paste information from Excel into a webpage and then push button. Will eventually need to get this all the way to having it print a PDF into a folder. I attempted the code below, but am completely unsure why it is not working. Google didn't resolve.
Website: https://www.easymapmaker.com/advanced
Attempted Code:
Sub MapMacro()
AddressGrid = Range("A1").Value
Set IE = CreateObject("InternetExplorer.Application")
WebSite = "https://www.easymapmaker.com/advanced"
With IE
.Visible = True
.navigate WebSite
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
On Error Resume Next 'This is here in case fields cant be found.
Set Address = IE.Document.getElementsByID("sourceData")
Address.Value = AddressGrid
Set Element = IE.Document.getElementsByID("optionButton")
Element.Click
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
End With
End Sub
TO clarify, I got this working by changing the lines:
Set Address = IE.Document.getElementsByID("sourceData")
Address.Value = AddressGrid
to:
IE.Document.getElementByID("sourceData").Value = AddressGrid
Now, since AddressGrid is only referring to one cell but you want a range, say "A1:D4" you could try this method:
Dim clipTXT As MSForms.DataObject
Range("A1:D4").Copy
Set clipTXT = New MSForms.DataObject
clipTXT.GetFromClipboard
IE.Document.getElementByID("sourceData").Value = clipTXT.GetTxt
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.