如何暂停Excel VBA并在循环之前等待用户交互

时间:2016-12-13 15:50:33

标签: excel vba excel-vba

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

我的问题是,在我现在所拥有的结尾处,如何让系统等我填写最后一页,然后一次我提交它意识到它已经提交并且循环回到开头,递增电子表格中的行,以便我们可以重新开始并为下一个学生再做一遍?

你可能会告诉我我不是程序员,只是一位音乐老师,他不会为所有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

1 个答案:

答案 0 :(得分: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