2016-12-13 36 views
0

我有一个非常基本的脚本,我正在使用Excel电子表格来填充表单。它将数据插入字段,然后等待我单击每个页面上的提交按钮。然后等待页面加载并填充下一组字段。我点击提交,下一页加载等。最后,在最后两页我必须做一些手动输入,我不能用电子表格做。到目前为止的代码如下所示。如何暂停Excel VBA并在循环之前等待用户交互

我的问题是,在我现在有没有结束,我怎样才能使系统等我填写该最后一页,那么一旦我提交意识到,它已经提交并循环回到开头,递增电子表格上的行,以便我们可以重新开始并为下一个学生再次完成整个任务?

正如你可能会告诉我,我不是一个程序员,只是一个音乐老师谁没有品味到为我的所有200名学生手动填写这些表单的想法,并获得了大部分从你看到的代码一个教程。


Function FillInternetForm() 
    Dim IE As Object 
    Set IE = CreateObject("InternetExplorer.Application") 
'create new instance of IE. use reference to return current open IE if 
'you want to use open IE window. Easiest way I know of is via title bar. 
    IE.Navigate "https://account.makemusic.com/Account/Create/?ReturnUrl=/OpenId/VerifyGradebookRequest" 
'go to web page listed inside quotes 
    IE.Visible = True 
    While IE.busy 
    DoEvents 'wait until IE is done loading page. 
    Wend 
     IE.Document.All("BirthMonth").Value = "1" 
      IE.Document.All("BirthYear").Value = "2000" 
    IE.Document.All("Email").Value = ThisWorkbook.Sheets("queryRNstudents").Range("f2") 
    IE.Document.All("Password").Value = ThisWorkbook.Sheets("queryRNstudents").Range("e2") 
     IE.Document.All("PasswordConfirm").Value = ThisWorkbook.Sheets("queryRNstudents").Range("e2") 
      IE.Document.All("Country").Value = "USA" 
    IE.Document.All("responseButtonsDiv").Click 

newHour = Hour(Now()) 
newMinute = Minute(Now()) 
newSecond = Second(Now()) + 3 
waitTime = TimeSerial(newHour, newMinute, newSecond) 
Application.Wait waitTime 

     IE.Document.All("FirstName").Value = ThisWorkbook.Sheets("queryRNstudents").Range("a2") 
    IE.Document.All("LastName").Value = ThisWorkbook.Sheets("queryRNstudents").Range("b2") 
    IE.Document.All("Address1").Value = "123 Nowhere St" 
    IE.Document.All("City").Value = "Des Moines" 
    IE.Document.All("StateProvince").Value = "IA" 
    IE.Document.All("ZipPostalCode").Value = "50318" 



End Function 
+0

另外,不使用所有的,作为其可能的,你可以有2个具有相同ID的控制或多个,使用getElementsByID ....标签名......的ClassName和从使用第x数组元素它,除非计划是如果有多于1的所有人 –

回答

1

我会使用IE浏览器的事件,更具体的形式,这样的事情,使用MSHTML控件库。

Private WithEvents IEForm As MSHTML.HTMLFormElement 

Public Sub InternetExplorerTest() 

Dim ie As SHDocVw.InternetExplorer 
Dim doc As MSHTML.HTMLDocument 

Set ie = New SHDocVw.InternetExplorer 
ie.Visible = 1 
ie.navigate "http://stackoverflow.com/questions/tagged/vba" 

While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy 
    DoEvents 
Wend 

Set doc = ie.document 
Set IEForm = doc.forms(0) 

End Sub 

Private Function IEForm_onsubmit() As Boolean 
    MsgBox "Form Submitted" 
End Function