【发布时间】:2017-01-26 04:51:41
【问题描述】:
我是 vba 和编码的新手,所以我需要你们的帮助。 此 vba 代码的目的: 1.打开互联网探索 2.输入我的用户名和密码并登录。 3.选择当前日期。 4. 从第一个下拉列表中选择一个特定选项(比如 X1)。 5. 然后从第二个下拉菜单中选择特定选项(比如选择 X1 后出现的 Y1,如果我选择 X2 不同的选项将出现在第二个下拉菜单中)。 6. 然后在第三个下拉列表中选择 Z1 选项,只有当我在第二个下拉列表中选择 Y1 时才会出现。 7.然后点击“保存”我的选择
现在,我编写了一个代码,它可以正确执行到第 4 步。但之后它无法在第 2 和第 3 下拉菜单中选择正确的选项。 不知道为什么。
我正在使用的代码:
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_MAXIMIZE = 3
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2
Private Sub Merchtimetracker_Click()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
On Error GoTo Err_Clear
With ie
.Visible = True
apiShowWindow ie.hwnd, SW_MAXIMIZE
.navigate "https://xxxxxxx"
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
End With
Set emailid = ie.document.getelementbyid("emailid")
emailid.Value = "xxxxxx"
Set Password = ie.document.getelementbyid("password")
Password.Value = "xxxxx"
ie.document.getelementsbyname("login_now")(0).Click
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
Do While ie.readyState <> 4 Or ie.Busy = True
DoEvents
Loop
ie.document.getelementbyid("datepicker").Value = Format(Date, "yyyy-mm-dd") 'write "Format (Date, "yyyy-mm-dd")" when want to give today's date and if not then just write ("2016-09-13")
Set project = ie.document.getelementbyid("project")
For i = 0 To project.Options.Length - 1
If project.Options(i).Text = "X1" Then
project.selectedindex = i
For j = 0 To task.Options.Length - 1
If task.Options(j).Text = "Y1" Then
task.selectedindex = j
Exit for
End If
Next j
Exit For
End If
Next i
End subcode
【问题讨论】:
标签: javascript html excel vba drop-down-menu