直接从用户表单

时间:2016-07-14 18:19:10

标签: vba excel-vba excel

我正在尝试添加一些代码以弹出用户窗体,允许最终用户输入特定网站的登录名/密码。这段代码Passing variable from Form to Module in VBA让我更接近我的目标,但我不知道如何让它按照我需要的方式工作。这是我的userform的代码。

Private Sub CommandButton1_Click()
UPass = UserForm1.UserID
Unload UserForm1
End Sub

Private Sub CommandButton1_Click()
UID = UserForm1.WavePassword
Unload UserForm1
End Sub

我在代码中使用以下内容登录网站。

    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

我遇到的问题是我在uid / upass变量上遇到“预期的语句结束”错误。如何正确获取userform将输入直接传递给变量,以便变量可用于登录网站?如果有更好的方法,我完全愿意改变方法。

4 个答案:

答案 0 :(得分:3)

这不可能编译:

Private Sub CommandButton1_Click()
UPass = UserForm1.UserID
Unload UserForm1
End Sub

Private Sub CommandButton1_Click()
UID = UserForm1.WavePassword
Unload UserForm1
End Sub

程序不能存在两次。重命名按钮OkButton,添加一些CancelButton并重写您的表单代码隐藏,如下所示:

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

通知OkButtonCancelButtonQueryClose处理程序只能隐藏表单,因此调用代码仍然可以读取IsCancelledUIDPWD属性值。

调用代码可以执行此操作 - 假设UserForm已重命名为LoginPrompt

Public Sub DownloadStuff()
    With New LoginPrompt
        .Show vbModal
        If .IsCancelled Then Exit Sub
        ConnectToWave .UID, .PWD
    End With
End Sub

最后,ConnectToWave程序,取用户的输入:

Private Sub ConnectToWave(ByVal userID As String, ByVal password As String)
    ' there, you got your values from the form - now use them!
End Sub

答案 1 :(得分:1)

我不确定driver是什么,但这句话是错误的

UID = driver.findElementByName("PASSWORD").SendKeys UID因为Sendkeys是一种方法,因此在尝试分配返回值时,您需要使用括号。

试试这个:

UID = driver.findElementByName("PASSWORD").SendKeys(UID)

答案 2 :(得分:1)

此:

Dim UID As String
    UID = driver.findElementByName("PASSWORD").SendKeys UID
Dim UPass As String
    UPass = driver.findElementByName("PASSWORD").SendKeys Upass

应该是:

Dim UID As String
    UID = driver.findElementByName("PASSWORD").SendKeys(UID)
Dim UPass As String
    UPass = driver.findElementByName("PASSWORD").SendKeys(Upass)

如果您正在调用的函数不会将任何内容分配回某个值,那么您不需要使用括号,但如果它要为变量赋值,那么您需要使用而是上面的语法。

其中Foo()是一个函数,而Bar是一个变量

'// Not assigning a value
Foo Bar

'// Assigning a value
someVar = Foo(Bar)

答案 3 :(得分:0)

要完成您想要的任务,您必须在模块顶部创建一个全局变量。我怀疑你是否正在使用ArcObjects,因此废弃了整个driver.findElementByName内容。此外,您已经正确设置了用户名和密码字段的值(此位:ie.Document.getElementById("txtLoginUsername").Value = UID),因此不需要任何SendKeys方法。

您需要的是代码模块顶部的类似内容:

Option Explicit
Public UID as String
Public UPass as String
相关问题