How do I link data from a user specific webpage, i.e one you need to be logged in on (for example Instagram followers or Facebook friends) etc to a spreadsheet?
From what I can see, the Data -> From Web option only lets you do it from open webpages, i.e those that you don't need to log in and is not specific to the user.
Is there a way I can do this on Excel?
This should be enough to get you started.
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
http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html
Related
I am writing a vba to login to a website.
I am getting compilation error in below statement.
getting error in the username and pwd adding part
Dim HMTLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub daily()
'
' daily Macro
'
Dim MyHTML_Element As IHTMLElement
Dim MYURL As String
On Error GoTo Err_Clear
' website
MYURL = ""
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MYURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
' user login and password
' error here
HTMLDoc.all.user-login.Value = ***
HTMLDoc.all.user-password.Value = ***
For Each MyHTML_Element In HTMLDoc.getElementsbyTagName("input")
'click submit to login
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
'gives debug error*** If MyHTML_Element.Type = "Reports" Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
You first need to add these references:
Microsoft HTML Object Library
Microsoft Internet Controls
Your If statements need to use multiple line syntax to do more than one thing in the If.
And notice how VBA adds a space to this?
HTMLDoc.all.user -login.Value
That's because objects cannot have a - in the name, so it seems you have the wrong property.
I have tried to guess at what your other problems were and changed it to this to at least get you going in the right direction, but you still need to change those to the correct property names.
Dim HMTLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub daily()
'
' daily Macro
'
Dim MyHTML_Element As IHTMLElement
Dim MYURL As String
On Error GoTo Err_Clear
' website
MYURL = ""
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MYURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
' user login and password
HTMLDoc.all.user -login.Value = "athi"
HTMLDoc.all.user -Password.Value = "pw123"
' changed this part
For Each MyHTML_Element In HTMLDoc.getElementsbyTagName("input")
If MyHTML_Element.Type = "submit" Then
MyHTML_Element.Click
Exit For
End If
If MyHTML_Element.Type = "Reports" Then MyHTML_Element.Click
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
Login into website
There are plenty of examples of how to login into a website, like gmail
But for some reason my code only opens the webpage without logging into the target page, using the provided username and password
I've enabled the HTML object library and Microsoft Internet Controls
Any thoughts , as I am fairly new to this object library?
Plus, how do I grab a table to import into excel from a website after I log in?
I tried macro recording but it needs a password to login into the website
An example would be helpful :) Thanks so much!
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub MyGmail()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://www.google.com/accounts/Login"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.Navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.Document
HTMLDoc.all.Email.Value = "nasterpizza87#gmail.com"
HTMLDoc.all.passwd.Value = "************" 'my password would be here
For Each MyHTML_Element In HTMLDoc.getElementsByTagName(“input”)
If MyHTML_Element.Type = “submit” Then _
MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
Sub LoginToSite()
Const cURL = "https://singlepoint.usbank.com/cs70_banking/logon/sbuser"
Const ccompanyID = "YourCustomerIDHere"
Const cj_username = "YourUserIDHere"
Const cj_password = "YourPasswordHere"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim CustomerIDInputBox As HTMLInputElement
Dim UserIDInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim objElement As Object
Dim objCollection As Object
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate cURL
'Wait for initial page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc = IE.document
'Get the only form on the page
Set LoginForm = doc.forms(0)
'Get the CustomerID textbox and populate it
'input name="companyID" id="companyID" size="18" value="" type="text"
Set CustomerIDInputBox = doc.getElementById("companyID")
UsernameInputBox.Value = ccompanyID
'Get the UserID textbox and populate it
'input name="j_username" id="j_username" size="18" type="text"
Set UserIDInputBox = doc.getElementById("j_username")
UsernameInputBox.Value = cj_username
'Get the Password textbox and populate it
'input name="j_password" id="j_password" size="18" type="password"
Set PasswordInputBox = doc.getElementById("j_password")
PasswordInputBox.Value = cj_password
'Get the form input button and click it
'input name="submit_logon" size="18" type="image"
Set ElementCol = IE.document.getElementsByName("submit_logon")
ElementCol.Item(0).Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
If it's specifically Gmail that you're trying to login to then the below code will work. I use it.
Sub Gmail()
'Set a reference (Visual Basic Editor) > Tools > References) to the following libraries:
' a) Microsoft Internet Controls
' b) Microsoft HTML Object Library
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLdoc As New MSHTML.HTMLDocument
Dim HTMLelement As MSHTML.IHTMLElement
With IE
.Visible = True
.Silent = True 'avoid any pop-up
.navigate "https://www.google.com/accounts/Login"""
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Call myTimer
Set HTMLdoc = IE.document
HTMLdoc.all.identifier.Value = "YourUsernameHere"
HTMLdoc.all.identifierNext.Click
With IE
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Call myTimer
For Each HTMLelement In HTMLdoc.getElementsByName("password")
If HTMLelement.getAttribute("type") = "password" Then
HTMLelement.Value = "YourPasswordHere"
Exit For
End If
Next HTMLelement
HTMLdoc.all.passwordNext.Click
Set IE = Nothing
Set HTMLdoc = Nothing
Set HTMLelement = Nothing
End Sub
Private Sub myTimer()
Dim timerStart As Single
Const pauseTIME As Integer = 1 'second
timerStart = Timer
Do Until Timer - timerStart > pauseTIME
DoEvents
Loop
End Sub
I want to use vba to login to a site. This makes it easy for everyone and not everyone has to know the password this way.
However, the site was recently updated and now the code that I used (that was duct taped together from bits and pieces)
`Sub apiweb()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://apiweb.biomerieux.com/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.login.Value = "xxxx"
HTMLDoc.all.Password.Value = "yyyy"
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub`
So it used to work that you press a button in excel, the site would open and after a second or 2 the login and password would show up and you'd just press login. Now it only opens the site.
I tried a few things including inspecting the elements, but i'm a novice so I don't really know what to look for or what to do. So any help (and explanation) would be appreciated!
How about this method?
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://apiweb.biomerieux.com/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.signupEmail.Value = "signupEmail"
HTMLDoc.all.signupPassword.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("signupSubmit")
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
As an aside, hit F12 to see the HTML behind your browser.
Option Explicit
Sub WbClkMe()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "https://apiweb.biomerieux.com/login"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementById("signupEmail") ' This is based on on your website
HTMLInput.Value = "" 'Put the value of Usernamae
Set HTMLInput = HTMLDoc.getElementById("signupPassword") 'This is based on on your website
HTMLInput.Value = "" 'Put the value of Password
Set HTMLButton = HTMLDoc.getElementById("signupSubmit") 'This is based on on your website
HTMLButton.Click
End Sub
I have code to open IE, login with username/password, tick some checkboxes, click button and open a report on a pop up window.
I am trying to copy the content of the pop up window to an Excel sheet. The code that deals with the pop up window doesn't detect the url.
Sub MyRosterApps()
Dim MyHTML_Element As IHTMLElement
Dim HTMLdoc As HTMLDocument
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim objIE As InternetExplorer, t As Date, nodeList As Object, i As Long
Dim ieIEWindow As SHDocVw.InternetExplorer
Dim y As Integer
Const MAX_WAIT_SEC As Long = 5
'Ignore Errors
On Error GoTo Err_Clear
MyURL = "Not a public URL"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
'Wait till finished
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLdoc = MyBrowser.document
'Enter user / password
HTMLdoc.all.txtUsername.Value = "username" ' Eneter your email id here
HTMLdoc.all.txtPassword.Value = "password" 'Enter your password here
HTMLdoc.all.Item("btnLogin").Click
For Each MyHTML_Element In HTMLdoc.getElementsByName("btnLogin")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
Do
'Wait till finished
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLdoc = MyBrowser.document
HTMLdoc.all.Item("ucSkillsPicker_lstSkills_35").Click
HTMLdoc.all.Item("btnShowReport").Click
' The Pop Up windows opens
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLdoc = MyBrowser.document
' Until here is works fine
' Trying to deal with the Pop Up windows
With objIE
.Visible = True
' I am trying to get the active window url which is the pop up
.navigate ieIEWindow.LocationURL
Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
t = Timer
Do
DoEvents
On Error Resume Next
' Go thru DIV classes and find these two oReportCell .a71
Set nodeList = .document.querySelectorAll("#oReportCell .a71")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While nodeList Is Nothing
If Not nodeList Is Nothing Then
'Paste the value found in DIV classes in Sheet1
With ThisWorkbook.Worksheets("Sheet1")
For i = 0 To nodeList.Length - 1
.Cells(1, i + 1) = nodeList.Item(i).innerText
Next
End With
End If
.Quit
End With
Err_Clear:
If Err <> 0 Then
'Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
This code below is separate from above. If I run just like this it does what I need. Of course, I have to paste the targeted window url.
.navigate "url of the pop up window"
The issue is that the pop up window url expires or something like that.
After some time the page says that I have to go through selecting some check boxes and submit.
Option Explicit
Public Sub GrabLastNames()
Dim objIE As InternetExplorer, t As Date, nodeList As Object, i As Long
Const MAX_WAIT_SEC As Long = 5
Set objIE = New InternetExplorer
With objIE
.Visible = True
' Paste targeted window. In my case is the pop up window
.navigate "url of the pop up window"
Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
t = Timer
Do
DoEvents
On Error Resume Next
Set nodeList = .document.querySelectorAll("#oReportCell .a71")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While nodeList Is Nothing
If Not nodeList Is Nothing Then
With ThisWorkbook.Worksheets("Sheet3")
For i = 0 To nodeList.Length - 1
.Cells(1, i + 1) = nodeList.item(i).innerText
Next
End With
End If
.Quit
End With
End Sub
I figured out how to automate a login using vba, but I would like to do the same with an embedded webbrowser. I have tried various methods found online, but am not having success.
Could anyone point me in the right direction?
Here is what I have currently. I'd like to make it work on "sheet1.webbrowser1"
Dim HTMLDoc As Object
Dim oBrowser As InternetExplorer
Sub Website_Login_Test()
Dim oHTML_Element As Object
Dim sURL As String
On Error GoTo Err_Clear
sURL = "http://example.com/login.action?os_destination=%2Fhomepage.action"
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.os_username.Value = "this"
HTMLDoc.all.os_password.Value = "this"
HTMLDoc.all.os_cookie.Click
HTMLDoc.all.login.Click
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
Dim oBrowser As Object
Set oBrowser = Sheet1.WebBrowser1
oBrowser.Navigate "http://www.google.com"