vba login to internet

wxue

New Member
Joined
May 13, 2008
Messages
35
how can I login to a website using vba. I googled many examples but still failed. any one can help me? Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Great Stuff Mr.John_w. I was looking this exactly but can u pls help me to import the contents of web page we opened using a query table or some other stuff..


i will b waiting for ur help, and u can also contact me my yahoo id is naveen8878 AT yahoo DOTcom pls help me in this regard


with pleasure
NAveen
 
Last edited by a moderator:
Upvote 0
I tried using this code on the Morgan Stanley financial site (https://www.morganstanleyclientserv...untSummary/AccountSummary/AccountSummary.aspx)

But got an error in this line (Run-time errir '91': Object variable or Wirh block variable not set)

UserIdBox.Value = cUserID

I am guessing it is because the username field isn't named "UserName".

How do I determine what the username field is named?

Incidentally, I get the same error with the password line

PasswordBox.Value = cPwd

Thanks for any help
 
Upvote 0
If you are using IE8 you can press F12, that should open a new window with the HTML source and it will also give search options and other functionality.
 
Upvote 0
I've been following this and had the same problem with the username and password field. I get the Run-time error 91 no object. I've looked at the source and believe I've found the names for the input boxes to be "userid" and "password" Am I missing something?

Here is the login page I'm looking to use:

https://nes.ncdc.noaa.gov/sub-login.html
 
Upvote 0
DD. Can you post your current code? It will be easier to edit what you have as opposed to writing up an example from scratch...

Thanks,
Tom
 
Upvote 0
Thanks for the quick reply. It's almost exactly the same as what the others on this thread have been using, just changed the form names.

Sub Test()

Const cURL = "https://nes.ncdc.noaa.gov/sub-login.html"
Const cUser = "XXXX" 'REPLACE XXXX WITH YOUR USER ID
Const cPass = "YYYY" 'REPLACE YYYY WITH YOUR PASSWORD

Dim ie As Object
Dim doc As HTMLDocument
Dim PageForm As HTMLFormElement
Dim UserIdBox As HTMLInputElement
Dim PasswordBox As HTMLInputElement
Dim FormButton As HTMLInputButtonElement
Dim Elem As IHTMLElement

Set ie = CreateObject("InternetExplorer.Application")

ie.Visible = True
ie.navigate cURL

'Wait for initial page to load

Do While ie.busy: DoEvents: Loop

Set doc = ie.document

'Output HTML tags to debug window

Debug.Print "Login page: " & ie.LocationURL
For Each Elem In doc.all
'Debug.Print Elem.tagName
Next

'Get the only form on the page

Set PageForm = doc.forms(0)

'Get the User Id textbox
'< input class="TextBox" maxlength="8" name="userid" size="8">

Set UserIdBox = PageForm.elements("userid")


'Set the User Id

UserIdBox.Value = cUser

'Get the password textbox
'< input class="TextBox" type="password" maxlength="10" name="Password" size="12">

Set PasswordBox = PageForm.elements("password")

'Set the password

PasswordBox.Value = cPass

'Submit the form (like clicking the 'Submit' button) to navigate to next page

PageForm.submit

'Wait for the new page to load

Do While ie.busy: DoEvents: Loop

'Get the HTML document of the new page

Set doc = ie.document

'Output HTML tags to debug window to prove this is the new page

Debug.Print "Terms of Use page: " & ie.LocationURL
For Each Elem In doc.all
'Debug.Print Elem.tagName
Next

'The new page contains 'Terms of Use' conditions and an 'Accept' button within a form
'Get the only form on the page

Set PageForm = doc.forms(0)

'Get the form submit button and click it to navigate to next page
'< input type="submit" value="Accept" name="selection">
'Note: unlike the login page, can't use PageForm.submit to submit this form because it doesn't have
'a method="post" attribute

Set FormButton = PageForm.elements("selection")
FormButton.Click

'Wait for the new page to load

Do While ie.busy: DoEvents: Loop

'Get the HTML document of the new page

Set doc = ie.document

'Output HTML tags to debug window to prove this is the new page

Debug.Print "Main Pronto page: " & ie.LocationURL
For Each Elem In doc.all
'Debug.Print Elem.tagName
Next

End Sub

Sub VisitWebsite()

Const Hyper As String = "https://nes.ncdc.noaa.gov/sub-login.html"
ThisWorkbook.FollowHyperlink Address:=Hyper ',
NewWindow = True


End Sub
Thanks for any help you can provide.
 
Upvote 0
See <A HREF="http://support.microsoft.com/kb/167658" TARGET="_blank">How To Automate Internet Explorer to POST Form Data</A> for an example of how to post form data.

This is your Post Data:
SaFormName=login__Fsub_login_htm&SaFormName=login__Fsub_login_htm&userid=YOURUSERNAME&password=YOURPASSWORD&B1=+++OK+++

This is your URL:
https://nes.ncdc.noaa.gov/cgi-bin/subs/sub-login.pl
 
Upvote 0

Forum statistics

Threads
1,225,204
Messages
6,183,574
Members
453,170
Latest member
sameer98

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