2016-06-07 178 views
0

enter image description here 我的代碼:如何在網頁上選擇下拉菜單中的值:VBA

Sub login() 
    Dim IE As Object 
    Dim HTMLDoc As Object, HTMLDoc2 As Object 
    Dim objCollection As Object 

    Const navOpenInNewTab = &H800 
    Set IE = CreateObject("InternetExplorer.Application") 
    IE.Visible = True 
    IE.Navigate "https:/com/1/19/login.esp" 

    Do While IE.Busy Or IE.ReadyState <> 4: Loop 

    Set HTMLDoc = IE.Document 

    With HTMLDoc 
    HTMLDoc.getElementById("USERNAME").Value = "xxxx" 'Entering credential 
    HTMLDoc.getElementById("PASSWORD").Value = "yyyyy" 
    End With 


    Set objCollection = IE.Document.getElementById("loginbutton") 
    objCollection.Click 

'Second webpage 

    Do While IE.Busy Or IE.ReadyState <> 4: Loop ' opening the second webpage 

    Set HTMLDoc2 = IE.Document 
    With HTMLDoc2 
    **HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu 
    HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange")** 
    End With 

    Set objCollection = IE.Document.getElementById("loginbutton") 
    objCollection.Click 

End Sub 

Q)代碼更改做我做的選擇Dwell_DF選項值1567? 上面的代碼給出了運行時錯誤'424':需要對象。

HTMLDoc2.getElementById("DEPARTMENTID").selectedindex = 1 'Drop down menu 
      HTMLDoc2.getElementById("DEPARTMENTID").FireEvent ("onchange") 

上面的行給出錯誤。

在第一個網頁中,我填寫登錄憑證,然後在下一頁中粘貼該圖像的圖像。在這裏我想更改下拉菜單中的值。

+0

什麼行會拋出錯誤? – OpiesDad

回答

0

試試看。值「1567」對應於InnerText「Dwell_DF」。

With HTMLDoc2 
    .getElementById("DEPARTMENTID").Focus 
    .getElementById("DEPARTMENTID").Value = "1567" 'You can also loop to find the text of the Option 
    .getElementById("DEPARTMENTID").FireEvent ("onchange") 
End With 
0

它應該是這樣的。

Sub passValueToComboBox1() 
    Dim ie As Object 
    Dim oHTML_Element As IHTMLElement 

    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = True 
    ie.navigate "http://your_URL_here.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 = "FUBU7" 

    For Each oHTML_Element In ie.document.getElementsByTagName("input") 
     If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For 
    Next 
End Sub 

查看下面的鏈接,瞭解如何以編程方式與網站進行交互的其他想法。

http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html