2013-07-01 37 views
6

我想用VBA將數據從Excel提交到網上的webform。該問題發生在*區域,我嘗試選擇並更新表單。我嘗試了各種選項(通過ID,名稱等getelement)沒有任何成功。我認爲這個問題與確定適當的因素有關。我見過有關在VBA中使用Locals功能的建議,但我不確定如何將其用於此目的。我有一種感覺,有經驗的人可以通過查看網站上的源代碼,使用VBA中的本地語言或其他一些技術,很快得出結論。VBA在線輸入數據並提交表格

表單已設置,所以所有字段都是文本,我可以在線輸入/提交數據,沒有問題。

在此先感謝您的任何幫助/建議。

Dim IE As Object 

Sub submitFeedback3() 
Application.ScreenUpdating = False 

Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True 
IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/" 

Application.StatusBar = "Submitting" 
    ' Wait while IE loading... 
While IE.Busy 
DoEvents 
Wend 
********************************************************************** 
IE.Document.getElementById("Form_Attempts-1372643500")(0).Value = "1" 
IE.Document.getElementById("submit-1-1372643500")(0).Click 
********************************************************************** 
Application.StatusBar = "Form Submitted" 
IE.Quit 
Set IE = Nothing 

Application.ScreenUpdating = True 
End Sub 
+0

該網頁嘗試在IE8 –

+0

可以輸入數據完全破碎/提交?這是此網頁的唯一目的。 –

+0

我看到的只有十幾個未標記的文本框和一個「提交」按鈕。看看HTML雖然,你最好試着根據它們的名字填充文本框(看起來是不變的),而不是它們的ID(它看起來有一個不斷變化的數字後綴) –

回答

4

下面的代碼

Dim IE As Object 

Sub submitFeedback3() 
    Application.ScreenUpdating = False 

    Set IE = CreateObject("InternetExplorer.Application") 
    IE.Visible = True 
    IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/" 

    Application.StatusBar = "Submitting" 
    ' Wait while IE loading... 
    While IE.Busy 
     DoEvents 
    Wend 
    ' ********************************************************************** 
    delay 5 
    IE.Document.getElementById("experience-1372700847").Value = "ddddd1" 
    delay 5 
    IE.Document.getElementById("Form_Time_Best-1372700847").Value = "ddddddddddd2" 
    delay 5 
    IE.Document.getElementById("submit-1-1372700847").Click 
    '********************************************************************** 
    Application.StatusBar = "Form Submitted" 
    IE.Quit 
    Set IE = Nothing 

    Application.ScreenUpdating = True 
End Sub 

Private Sub delay(seconds As Long) 
    Dim endTime As Date 
    endTime = DateAdd("s", seconds, Now()) 
    Do While Now() < endTime 
     DoEvents 
    Loop 
End Sub 
+0

我爲表單使用了不同的設置,並讓所有的工作都完美無缺。謝謝你的建議。 –

+0

我使用相同的代碼。最後,它點擊一個按鈕導航到一個新的頁面。一旦完成,我不能訪問新頁面的元素。我可能會錯過什麼。 – Richa

+0

@Richa你應該確保你的頁面已經完成加載。我創建了一個簡單的子來幫助完成此操作:Sub IEBusy(IE_Object As Object)''當IE_Object.Busy或IE_Object.ReadyState <> 4''DoEvents'' Loop''Exit Sub''執行時。您只需使用「調用IEBusy(IE)」,就可以很簡單地等待頁面完成加載。 –