问题
I am trying to add some code to have a userform pop up which allows the end user to input their login/password for s specific website. This code Passing variable from Form to Module in VBA got me closer to my goal but I am not sure how to make it work the way I need to. Here is the code for my userform.
Private Sub CommandButton1_Click()
UPass = UserForm1.UserID
Unload UserForm1
End Sub
Private Sub CommandButton1_Click()
UID = UserForm1.WavePassword
Unload UserForm1
End Sub
And I am using the below in the code to log into the website.
Public Sub Connect_To_Wave()
Dim Dasboard As Worksheet
Set Dashboard = ActiveWorkbook.Worksheets("Dashboard")
Dim UID As String
UID = driver.findElementByName("PASSWORD").SendKeys UID
Dim UPass As String
UPass = driver.findElementByName("PASSWORD").SendKeys Upass
Set ie = CreateObject("InternetExplorer.Application")
my_url = "url of website - not a variable"
With ie
.Visible = True
.Navigate my_url
.Top = 100
.Left = 530
.Height = 700
.Width = 400
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
ie.Document.getElementById("txtLoginUsername").Value = ""
ie.Document.getElementById("txtLoginPassword").Value = ""
ie.Document.getElementById("txtLoginUsername").Value = UID
ie.Document.getElementById("txtLoginPassword").Value = UPass
ie.Document.getElementById("btnLogin").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End Sub
The issue I am running into is that I get an error of "expected end of statement" on the uid/upass variables. How do I correctly get the userform to pass the input directly into the variable so the variable can be used to log into the website? I am completely open to change the method if there is a better way as well.
回答1:
This can't possibly compile:
Private Sub CommandButton1_Click()
UPass = UserForm1.UserID
Unload UserForm1
End Sub
Private Sub CommandButton1_Click()
UID = UserForm1.WavePassword
Unload UserForm1
End Sub
A procedure cannot exist twice. Rename your button OkButton, add some CancelButton and rewrite your form's code-behind as follows:
Option Explicit
Private cancelling As Boolean
Public Property Get UID() As String
UID = UserID.Text
End Property
Public Property Get PWD() As String
PWD = WavePassword.Text
End Property
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelling
End Property
Private Sub OkButton_Click()
Me.Hide
End Sub
Private Sub CancelButton_Click()
cancelling = True
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
cancelling = True
Me.Hide
End If
End Sub
Notice OkButton, CancelButton and the QueryClose handler only ever Hide the form, so the calling code can still read the IsCancelled, UID and PWD property values.
That calling code could do this - assuming the UserForm is renamed to LoginPrompt:
Public Sub DownloadStuff()
With New LoginPrompt
.Show vbModal
If .IsCancelled Then Exit Sub
ConnectToWave .UID, .PWD
End With
End Sub
And last, the ConnectToWave procedure, taking the user's input:
Private Sub ConnectToWave(ByVal userID As String, ByVal password As String)
' there, you got your values from the form - now use them!
End Sub
回答2:
I am not sure what driver is but this statement is wrong
UID = driver.findElementByName("PASSWORD").SendKeys UID as Sendkeys is a method so when trying to assign the return value you need to use brackets.
Try this :
UID = driver.findElementByName("PASSWORD").SendKeys(UID)
回答3:
This:
Dim UID As String
UID = driver.findElementByName("PASSWORD").SendKeys UID
Dim UPass As String
UPass = driver.findElementByName("PASSWORD").SendKeys Upass
Should be:
Dim UID As String
UID = driver.findElementByName("PASSWORD").SendKeys(UID)
Dim UPass As String
UPass = driver.findElementByName("PASSWORD").SendKeys(Upass)
If you are calling a function that isn't assigning anything back to a value then you don't need to use parentheses, but if it's assigning something to a variable then you need to use the above syntax instead.
Where Foo() is a function and Bar is a variable
'// Not assigning a value
Foo Bar
'// Assigning a value
someVar = Foo(Bar)
回答4:
To accomplish what you want, you have to create a global variable at the top of your module. I doubt you are using ArcObjects so scrap the whole driver.findElementByName stuff. Besides, you already set the value of the username and password fields correctly (this bit: ie.Document.getElementById("txtLoginUsername").Value = UID) so there's no need for any SendKeys method.
What you need is something like this at the very top of your code module:
Option Explicit
Public UID as String
Public UPass as String
来源:https://stackoverflow.com/questions/38381451/assigning-a-variable-directly-from-a-userform