'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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
