2012-09-26 62 views
2

我想通過提供表單輸入文本框中的所有值來提交webform,但是當我通過excel vba調用提交按鈕時,其中一個文本框是變空了,並拋出驗證錯誤。當提交一個web表單在vba中輸入文本框的值爲空

Sub Click_Btn()  
    Dim objForms As Object 
    Dim vTxtInput As Variant 
    'Set objIE = GetIEApp 
    Set objIE = New InternetExplorer 

    'Make sure an IE object was hooked 
    If TypeName(objIE) = "Nothing" Then 
    MsgBox "Could not hook Internet Explorer object", _ 
     vbCritical, "GetFields() Error" 
    'GoTo Clean_Up 
    End If 
    objIE.Navigate "http://fly3.emirates.com/CAB/IBE/SearchAvailability.aspx" 
    objIE.Visible = True 

    Sleep 5000 

    Set objForms = objIE.document.all 

    'Choose one way Flights 
    objIE.document.getElementById("ctl00_c_CtWNW_onewaySearch").Click 
    Sleep 1000 

    objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Value = "Sydney (SYD)" 

    ' Departure 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Value = "Mumbai (BOM)" 

    ' Departure Date 
    objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Value = "28 Sep 12" 
    Sleep 1000 

    objIE.document.getElementById("ctl00_c_FS_FF").Click 
End Sub 

當我點擊窗體的ctl00_c_CtWNW_ddlTo-suggest文本框提交按鈕變得空,得到一個錯誤。

+0

+ 1有趣的問題:) –

回答

0

你得到這個錯誤,因爲你沒有給它足夠的時間來驗證FROMTO下拉框的名字(是 - 不是文本框)

試試這個代碼(屢試不爽)。另外我使用IE的Late Binding。根據您的代碼進行更改。

Sleep 5000在下拉列表中給文本足夠的時間用下拉列表驗證自己。

Option Explicit 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

Sub Click_Btn() 
    Dim objForms As Object 
    Dim vTxtInput As Variant 
    Dim objIE As Object 

    Set objIE = CreateObject("InternetExplorer.Application") 

    'Make sure an IE object was hooked 
    If TypeName(objIE) = "Nothing" Then 
     MsgBox "Could not hook Internet Explorer object", _ 
     vbCritical, "GetFields() Error" 
     'GoTo Clean_Up 
    End If 

    objIE.Navigate "http://fly3.emirates.com/CAB/IBE/SearchAvailability.aspx" 
    objIE.Visible = True 

    Sleep 5000 

    Set objForms = objIE.document.all 

    '~~> Choose one way Flights 
    objIE.document.getElementById("ctl00_c_CtWNW_onewaySearch").Click 

    '~~> From 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Focus 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Value _ 
    = "Mumbai (BOM)" 
    Sleep 5000 

    '~~> Departure Date 
    objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Focus 
    objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Value _ 
    = "28 Sep 12" 
    Sleep 5000 

    '~~> To 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Focus 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Value _ 
    = "Sydney (SYD)" 
    Sleep 5000 

    objIE.document.getElementById("ctl00_c_FS_FF").Focus 
    objIE.document.getElementById("ctl00_c_FS_FF").Click 
End Sub 

編輯

這裏是Way 2,這是快於Way 1 (Above)通過15 Seconds,因爲我們沒有使用Sleep 5000用於驗證。這並不要求你驗證下拉菜單。它所做的是繞過checkValidation(); javascript,它在提交按鈕的ONCLICK事件中執行。

WAY 2

Option Explicit 

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

Sub Click_Btn() 
    Dim objForms As Object 
    Dim vTxtInput As Variant 
    Dim objIE As Object 

    Set objIE = CreateObject("InternetExplorer.Application") 

    'Make sure an IE object was hooked 
    If TypeName(objIE) = "Nothing" Then 
     MsgBox "Could not hook Internet Explorer object", _ 
     vbCritical, "GetFields() Error" 
     'GoTo Clean_Up 
    End If 

    objIE.Navigate "http://fly3.emirates.com/CAB/IBE/SearchAvailability.aspx" 
    objIE.Visible = True 

    Sleep 5000 

    Set objForms = objIE.document.all 

    'Choose one way Flights 
    objIE.document.getElementById("ctl00_c_CtWNW_onewaySearch").Click 

    ' From 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Focus 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlFrom-suggest").Value _ 
    = "Mumbai (BOM)" 

    ' Departure Date 
    objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Focus 
    objIE.document.getElementById("ctl00_c_CtWNW_txtDepartDate").Value _ 
    = "28 Sep 12" 

    ' To 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Focus 
    objIE.document.getElementById("ctl00_c_CtWNW_ddlTo-suggest").Value _ 
    = "Sydney (SYD)" 

    objIE.document.getElementById("ctl00_c_FS_FF").Focus 
    objIE.document.getElementById("ctl00_c_FS_FF").onclick = _ 
    Replace(objIE.document.getElementById("ctl00_c_FS_FF").onclick, _ 
    "checkValidation();", "true;") 

    objIE.document.getElementById("ctl00_c_FS_FF").Click 
End Sub 
相關問題