Log onto Website via VBA

BlackHarry

New Member
Joined
Oct 9, 2017
Messages
3
Hi

I have the below code, it opens the website www.reece.com.au/myaccount/login, but does not fill in the email and password (Correct information on code has been removed for privacy)

I assume it is not working as I have not referenced the correct name in HTML. I assume it is email and password, but I may be wrong. I also am unable to find if MyHTML_Element.Type = "submit" is actually submit. Can anyone please look at the HTML and tell me if I have the wrong information in my VBA?


Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub MyReece()



Dim MyHTML_Element As IHTMLElement
Dim MyURL As String

On Error GoTo Err_Clear
MyURL = "https://www.reece.com.au/myaccount/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 = "Infousername"
HTMLDoc.all.password.Value = "Info password"
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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome to the forum! I hope this is not a hack request.

Normally, I don't see a need for Wait. The loop methods or waiting for a new url works for me. That site is the slowest I have seen.
Code:
Sub Test_LoginReece()
    LoginReece "kEmail", "kPassword"
End Sub

' Add references in Tools > References for:
' Microsoft HTML Object Library
' Microsoft Forms 2.0 Object Library
' Microsoft Internet Controls
Sub LoginReece(email As String, password As String)
    Const strURL_c As String = "https://www.reece.com.au/myaccount/login"
    Dim url As String
    Dim objIE As SHDocVw.InternetExplorer
    Dim ieDoc As MSHTML.HTMLDocument
    Dim tbxPwdFld As MSHTML.HTMLInputElement
    Dim tbxUsrFld As MSHTML.HTMLInputElement
    Dim btnSubmit As MSHTML.HTMLInputElement
    
    Excel.Application.Cursor = xlWait
    On Error GoTo Err_Hnd
    
    'Create Internet Explorer Object
    Set objIE = New SHDocVw.InternetExplorer
    'Navigate the URL
    objIE.navigate strURL_c
    objIE.Visible = True
    'Wait for page to load
    'https://msdn.microsoft.com/en-us/library/ms534361(v=vs.85).aspx
    Do Until objIE.readyState = READYSTATE_COMPLETE: Loop
    'Do: Loop Until objIE.readyState = READYSTATE_COMPLETE
    Set ieDoc = objIE.document
    Application.Wait Now + TimeValue("00:00:05")
    ieDoc.getElementsByName("email").Item(0).Value = email
    ieDoc.getElementById("password").Value = password
    ieDoc.getElementsByClassName("btn btn-primary btn-med ladda-button").Item(0).Click
    
Err_Hnd: '(Fail gracefully)
    objIE.Visible = True
    On Error GoTo 0
    Excel.Application.Cursor = xlDefault
End Sub
 
Upvote 0
Thank you so much Kenneth, works perfectly. I can assure you its not a hack. We are a plumbing company and we price all our jobs using excel, and the above is to allow us to get directly into the supplier pricing website with ease, to ensure all of figures are correct.
 
Upvote 0
I may be pushing it but thought I would ask. I also would like to hyperlink various hyperlinks once logged on.

ie
PVC pipe = "https://www.reece.com.au/myaccount/trade/products-and-pricing/product-search/?keyword=pvc+pipe"
PVC Bends = "https://www.reece.com.au/myaccount/trade/products-and-pricing/product-search/?keyword=pvc+bends"
etc

So basically, I can just click on a link in the sheet that says PVC pipe, it will automatically log me on and take me to the relevant page, but also log me on and take me to PVC Bends when I click on that part of the sheet.

For some reason if I just do the hyperlink without the above code (just a straight forward excel hyperlink), the hyperlink will take me the website log on screen, I then put in logon details manually, and then it says something went wrong and it wont log on. This is why I started the whole process of the above in the first place!
 
Upvote 0
I will have to think on that tomorrow.

Likely, what you want to do is to run a macro. It would first check to see if logged in, if not, login. Then it would use the window with the login and navigate to those. Your hypertext would be the one after =. e.g. pvc pipe. The built url would replace " " with "+". Other things could clue the macro in on what you want. You might want a prefix like: Reese - PVC Pipe.

The concept is shown here if you want to try. https://www.extendoffice.com/documents/excel/4422-excel-run-macro-from-hyperlink.html
 
Upvote 0
Hi Kenneth,

I was using this to update my VBA because it is needed, but i don't seem to be able to get this to write in the values for email and password automatically.

Can you think of any reason as to why this is??

The field is named "pf.pass" and i can't use the other code suggested for obvious reasons but i can't get this filled out either.

Any suggestion would be gratefully accepted.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top