【问题标题】:Drop down handling in a webpage using vba in internet explorer在 Internet Explorer 中使用 vba 在网页中进行下拉处理
【发布时间】: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


    【解决方案1】:

    这是将值传递给 ComboBox 的(大部分)简单方法。

    Sub passValueToComboBox1()
       Dim ie  As Object
       Dim oHTML_Element As IHTMLElement
    
       Set ie = CreateObject("InternetExplorer.Application")
       ie.Visible = True
       ie.navigate "http://peterschleif.bplaced.net/excel/combobox/index.php"
       While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
    
       Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)
       If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "com.db.moap.report.FUBU7"
    
       For Each oHTML_Element In ie.document.getElementsByTagName("input")
          If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
       Next
    End Sub
    

    【讨论】:

    • 感谢您的评论,但我无法理解您想说什么。我只是初学者,所以可能你写了最正确的版本,但我没有得到它。请您至少举一个例子使它更简单。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2013-11-24
    • 2021-05-21
    相关资源
    最近更新 更多