从userform直接分配变量 [英] Assigning a variable directly from a userform

查看:143
本文介绍了从userform直接分配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一些代码以使用户窗体弹出,允许最终用户输入他们的特定网站的登录/密码。此代码在VBA中将表单转换为模块让我更接近我的目标,但我不知道如何使其按照我需要的方式工作。这是我的用户窗体的代码。

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

我遇到的问题是我得到uid / upass变量的预期结束语句的错误。如何正确地获取用户窗体将输入直接传递到变量中,以便变量可以用于登录网站?如果有更好的方法,我完全可以改变方法。

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.

推荐答案

这不可能编译: / p>

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

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

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

注意 OkButton CancelButton QueryClose 只有隐藏表单,所以调用代码仍然可以读取 IsCancelled UID PWD 属性值。

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.

调用代码可以这个 - 假设UserForm被重命名为 LoginPrompt

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

最后, 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

这篇关于从userform直接分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆