Password not working - VBA

Shamas

Board Regular
Joined
Aug 24, 2006
Messages
64
Hi,

I am using an excel sheet (2003) which runs a lot of code. When I open it up a form collects my username & password before it will continue.

Once we complete this step part of the code loads up a webpage and has to log in with username & password (taken from the form) before copying the data and pasting it back in to my excel sheet.

The webpage (intranet) was moved and some details were changed in the background.

I amended these details in the code but it is no longer able to login when it fires up the webpage.

I tested the sheet by adding my username and password in at code level (replacing teh form fetch bit) and it worked. When I switch it back to getting the username and password from the input form it doesn't work again.

Can you help please?

Thanks

ps. sorry for the messy layout

CODE--------------------

Sub myWebQuery()
'Dates for URL manipulation
Application.DisplayAlerts = False
Dim dFrom As String, dTo As String
dFrom = Sheets("Update").Range("D4").Value
dTo = Sheets("Update").Range("D4").Value + 6
‘Update sheet contains the date from the Monday just gone
Sheets("callsSQL").Select
'Credentials
Dim userNameVar, passW As String
userNameVar = loginForm.uNameBox.Value
passW = loginForm.passBox.Value
‘This is the bit that is causing problems… If I swap the loginForm bits with actual name & password then it works fine

'URL
Dim webPath, webPathFinal As String
webPath = "http://reporting.xxxxxx.com"
‘blanked out
webPathFinal = "http://reporting.xxxxxx.com?startdate=" & dFrom & "&enddate=" & dTo & "&drill=agent"

'IE
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

With IE
.navigate webPath
.Visible = True

'wait until page loads up
Do Until Not .Busy
DoEvents
Loop

On Error Resume Next
'Object on the page
Dim userBox As Object
Set userBox = IE.document.all.Item("j_username")

userBox.Value = userNameVar

''pass
Dim passBox As Object
Set passBox = IE.document.all.Item("j_password")

passBox.Value = passW

'Button to login
Dim submitButton As Object
Set submitButton = IE.document.all.Item("submit")

submitButton.Click

'wait until page loads up
Do Until Not .Busy
DoEvents
Loop

'Refresh WebQuery clear First
Sheets("callsSQL").Select
Cells.Select
Selection.Value = ""

.navigate webPathFinal

'wait until page loads up
Do Until Not .Busy
DoEvents
Loop

Do Until Not .Busy
DoEvents
Loop

.ExecWB 17, 0 'Select everything on the page
.ExecWB 12, 2 'Copy everything on the page
Sheets("callsSQL").Select

ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False

'unmerge cells
Cells.Select
With Selection
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
'.Value = .Value
End With
Cells.EntireColumn.AutoFit

'To prevent exeption error with existing object log out the user

'wait until page loads up
Do Until Not .Busy
DoEvents
Loop

IE.Quit
End With

Set IE = Nothing
'clear objects
Call clearObjects

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
When you say "it doesn't work again", does the web page give a cause e.g. invalid password or something like that?

Maybe wrap the UserName and password in a Trim function to strip any leading or trailing space that may have crept in.
Code:
userNameVar = Trim(loginForm.uNameBox.Value)
passW = Trim(loginForm.passBox.Value)

As a test, maybe you should...
Code:
[COLOR="RoyalBlue"]Debug Print[/COLOR] userNameVar & " " & passW
...to confirm both variables are being set with the values
 
Upvote 0
Thanks!

I set the webpage to visible so I could see what happens - when I revert back to form data it doesn't seem to enter the username and password at all.

I tried Trim() - no success

Not sure how and where I can do this bit:

Debug Print userNameVar & " " & passW
 
Upvote 0
Correction:
It should have been Debug.Print

Instead of that, put a msgbox after you set userNameVar and passW just to confirm they are being set properly

Code:
userNameVar = Trim(loginForm.uNameBox.Value)
passW = Trim(loginForm.passBox.Value)
[COLOR="Red"]MsgBox "UserName: " & userNameVar & vbcr & "Password: " & passW[/COLOR]
 
Upvote 0
msgbox trick is genius.

Results came back blank... I can't work out why it doesn't pull the username & password through...
 
Upvote 0
We'll your making some progress. So that's good.

Do you have more than one userform?
Are theses the exact names of the TextBoxes; uNameBox, passBox?
Is this the exact name of the UserForm with the two TextBoxes: loginForm?
 
Upvote 0
Is the loginForm up and running while you call this Sub myWebQuery? And if yes, (I have to ask the obvious because I can't see what's happening) is there anything in the two textboxes?

How are you calling this Sub myWebQuery? Where is that code?
 
Upvote 0
I saved the Trim stuff. Closed excel down and logged on to another PC

This time I tried the sheet and it worked perectly!

I asked someone to use my sheet on their PC with their own username and password and it worked again!

Thanks for your help :)
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,217
Members
452,895
Latest member
BILLING GUY

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