2016-09-18 247 views
0

我是vba和編碼的新手,所以我需要你們的幫助。 這個vba代碼的purpOse: 1.打開網絡探索 2.把我的用戶名爲&的密碼和登錄。 3.選擇當前日期。 4.從第1個下拉列表中選擇一個特定的選項(比如X1)。 5.然後選擇特定的選項(比如選擇X1後出現的Y1,如果我從第二個下拉列表中選擇X2不同的選項將進入第二個下拉列表中)。 6.然後在第三個下拉列表中選擇Z1選項,僅當我在第二個下拉列表中選擇Y1時纔會出現。 7.然後點擊「保存」我的選擇在網頁瀏覽器中使用vba在網頁中下拉處理

現在,我已經wriiten代碼正確執行,直到第4步,但之後,它無法在第二和第三下拉選擇正確的選項。 不知道爲什麼。我使用

代碼:

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 

回答

0

這裏是傳遞一個值到一個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 
+0

感謝您的評論,但我無法理解你想說什麼。我只是初學者,所以可能是你寫了最正確的版本,但我沒有得到它。至少可以用一個例子來簡化它。你的幫助表示讚賞。 –