简体   繁体   中英

Drop down handling in a webpage using vba in internet explorer

I am new to vba and coding so, I need help from you guys. PurpOse of this vba code: 1. open internet explore 2. Put my user id & password and login. 3. select current date. 4. Select a particular option (say X1) from 1st dropdown. 5. Then select particular option (say Y1 which come after selecting X1, if i select X2 diffrent options will come in 2nd dropdown) from 2nd drop down. 6. then select Z1 option in 3rd dropdown which will come only if i am selecting Y1 in 2nd dropdown. 7. then click "save" my selections

Now, i have wriiten a code which correctly performs till step 4. but after that it is not able to select the correct option in 2nd and 3rd drop downs. Don't know why.

Code I am using:

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

Here is a (mostly) simple way to pass a value to a 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM