Log in to embedded webbrowser - excel

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"

Related

Logging in issue

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

Using vba to login

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

Setting 'New InternetExplorer' generates automation error: -2125463506 (8150002e)

I have VBA code to login to a website. Sometimes it works, but other times I get
automation error: 2125463506 (8150002e).
I have read a number of posts related to this topic, tried a number of different methods and still can't get it to work.
My OS is Windows 10. In Windows 7 I didnt get this error.
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub Repsol1()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim form As HTMLFormElement
MyURL = "https://login.repsol.com/es/Landing/AuthnPage?returnUrl=https://www.repsol.com/es_es/"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.Navigate MyURL
MyBrowser.Visible = True
Do
DoEvents
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.Document
Do While HTMLDoc.getElementById("gigya-login-form") Is Nothing
DoEvents
Loop
Set form = HTMLDoc.getElementById("gigya-login-form")
form.all.UserName.Value = "XXXXX#XXXXX"
form.all.Password.Value = "XXXXX"
form.getElementsByClassName("gigya-input-submit")(0).Click
Do
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate "https://www.repsol.com/es_es/aplicaciones/SO/WebEESS/default.aspx?usuario=XXXXX"
End With
End Sub
I believe you should use Set MyBrowser = New InternetExplorerMedium. IE updated its security within the browser.

How to link logged in webpages to Excel

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

Navigate to new URL in existing Internet Explorer window

I am trying to get an existing (open) IE window to load a new URL via ie.Navigate.
Below is my worksheet code which loads a website on click of a button:
Private Sub Sendit_Click()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.yahoo.com"
Do
Loop Until IE.ReadyState = READYSTATE_COMPLETE
GetIE
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
This code is in my module:
Function GetIE()
Dim shellWins As ShellWindows
Dim IE As InternetExplorer
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
' Get IE
Set IE = shellWins.Item(0)
Else
' Create IE
Set IE = New InternetExplorer
IE.Visible = True
End If
IE.navigate "http://www.google.com"
Set shellWins = Nothing
Set IE = Nothing
End Function
IE doesn't navigate to http://www.google.com.
Sub TestGetIE()
Dim IE As Object
Set IE = CreateObject("Internetexplorer.Application")
IE.Visible = True
IE.navigate "https://www.yahoo.com"
WaitFor IE
Set IE = GetIE("https://www.yahoo.com")
IE.navigate "http://www.google.com"
End Sub
Sub WaitFor(IE)
Do
Loop Until IE.readyState = READYSTATE_COMPLETE
End Sub
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
'check the URL and if it's the one you want then
' assign it to the return value
sURL = o.LocationURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function
As a similar/adapted alternative, here's a favorite function of mine.
This function returns an object representing the existing InternetExplorer instance if there is one, otherwise a newly-created instance.
Function GetIE() As Object
'return an object for the open Internet Explorer window, or create new one
For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
Next GetIE
If GetIE Is Nothing Then Set GetIE=CreateObject("InternetExplorer.Application")'Create
GetIE.Visible = True 'Make IE window visible
End Function
More information and example here.

Resources