我正致力于使用Excel电子表格中的数据填写在线表单。我在Excel中使用VBA模块这样做。
步骤如下: 1)导航到页面 2)填写详细信息 3)点击"继续"继续下一页 4)填写更多细节 5)点击"保存"保存页面
以下是代码:
Sub FillInternetForm()
'Header Start
Dim IE As Object
Dim ROW As Integer
Dim MAXROW As Integer
Set IE = CreateObject("InternetExplorer.Application")
MAXROW = 3
ROW = 2
'Header End
'Step 1 Navigate to the page
IE.Navigate *PAGE_1_URL*
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4
DoEvents
Wend
'Step 1 Ends
'Step 2&3 Fill in the details and click "continue"
IE.Document.ALL("ctl00_PageContent_PAYDPaymentMode").Value = "18"
IE.Document.ALL("ctl00_PageContent_PAYDPaymentDate").Value = Cells(ROW, 1).Value
IE.Document.ALL("ctl00_PageContent_PAYDBusinessUnit").Value = "104"
IE.Document.ALL("ctl00_PageContent_PAYDAmountPaid").Value = Cells(ROW, 2).Value
IE.Document.ALL("ctl00_PageContent_ContinueButton__Button").Click
While IE.Busy Or IE.ReadyState <> 4
DoEvents
Wend
'Step 2&3 Ends (IE moved to page 2)
'Step 4&5 Fill in more details and click "Save"
IE.Document.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
IE.Document.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
IE.Document.ALL("ctl00_PageContent_SaveButton__Button").Click
'Step 4&5 Ends
End Sub
我做了第1步到第3步。但是,当涉及到第4步时,我收到错误。错误是:运行时错误424需要对象。我知道这意味着模块无法在步骤4中找到元素。
错误正是:
IE.Document.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
在此过程必须终止之前。
要调试步骤4中的代码是否有任何问题,我将其取出并单独运行。我拿了Step4&amp; 5的整个部分并标题出来,然后导航到第2页。
像这样:
Sub FillInternetForm()
Dim IE As Object
Dim ROW As Integer
Dim MAXROW As Integer
Set IE = CreateObject("InternetExplorer.Application")
MAXROW = 3
ROW = 2
IE.Navigate *PAGE_2_URL*
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4
DoEvents 'wait until IE is done loading page.
Wend
IE.Document.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
IE.Document.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
IE.Document.ALL("ctl00_PageContent_SaveButton__Button").Click
End Sub
它有效。
我不知道是什么问题。我猜测所有的elementID都是在开始时加载的,而且没有加载第2页的ID?
有谁知道此问题的来源或解决方案?谢谢。
P.S。所有名称元素ID都是从webform的源代码中复制和粘贴的,应该是正确的。
编辑: 我在下面的评论中更新了这个问题。
Edit2:使用excel 2016在win10机器上尝试完全相同的代码。代码运行完美。感谢您提供的所有帮助。
答案 0 :(得分:0)
请试一试......(未经测试)
Sub FillInternetForm()
'Header Start
Dim IE As Object
Dim Doc As Object
Dim ROW As Integer
Dim MAXROW As Integer
Dim ContinueButton As Object
Dim SaveButton As Object
Set IE = CreateObject("InternetExplorer.Application")
MAXROW = 3
ROW = 2
'Header End
'Step 1 Navigate to the page
IE.Navigate PAGE_1_URL
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4 Or ContinueButton Is Nothing
DoEvents
Set Doc = IE.document
Set ContinueButton = Doc.ALL("ctl00_PageContent_ContinueButton__Button")
Wend
'Step 1 Ends
'Step 2&3 Fill in the details and click "continue"
Doc.ALL("ctl00_PageContent_PAYDPaymentMode").Value = "18"
Doc.ALL("ctl00_PageContent_PAYDPaymentDate").Value = Cells(ROW, 1).Value
Doc.ALL("ctl00_PageContent_PAYDBusinessUnit").Value = "104"
Doc.ALL("ctl00_PageContent_PAYDAmountPaid").Value = Cells(ROW, 2).Value
ContinueButton.Click
While IE.Busy Or IE.ReadyState <> 4 Or SaveButton Is Nothing
DoEvents
Set Doc = IE.document
Set SaveButton = Doc.ALL("ctl00_PageContent_SaveButton__Button")
Wend
'Step 2&3 Ends (IE moved to page 2)
'Step 4&5 Fill in more details and click "Save"
Doc.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
Doc.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
SaveButton.Click
'Step 4&5 Ends
End Sub
答案 1 :(得分:0)
将代码更新为:
Sub FillInternetForm()
'Header Start
Dim IE As Object
Dim DOC As Object
Dim ROW As Integer
Dim MAXROW As Integer
Dim SAVEBUTTON As Object
Dim COUNTER As Integer
Set IE = CreateObject("InternetExplorer.Application")
Set SAVEBUTTON = Nothing
MAXROW = 3
ROW = 2
COUNTER = 0
'Header End
'Step 1 Navigate to the page
IE.Navigate "*WEBSITE*"
IE.Visible = True
While IE.Busy Or IE.ReadyState <> 4
DoEvents
Wend
'Step 1 Ends
Set DOC = IE.Document
'Step 2&3 Fill in the details and click "continue"
DOC.ALL("ctl00_PageContent_PAYDPaymentMode").Value = "18"
DOC.ALL("ctl00_PageContent_PAYDPaymentDate").Value = Cells(ROW, 1).Value
DOC.ALL("ctl00_PageContent_PAYDBusinessUnit").Value = "104"
DOC.ALL("ctl00_PageContent_PAYDAmountPaid").Value = Cells(ROW, 2).Value
DOC.ALL("ctl00_PageContent_ContinueButton__Button").Click
'Step 2&3 Ends (IE moved to page 2)
While IE.Busy Or IE.ReadyState <> 4 Or SAVEBUTTON Is Nothing
DoEvents
If Not IE.Busy Or IE.ReadyState = 4 Then
IE.Refresh
End If
Set DOC = IE.Document
On Error Resume Next
Set SAVEBUTTON = DOC.ALL("ctl00_PageContent_SaveButton__Button")
COUNTER = COUNTER + 1
Wend
'Step 4&5 Fill in more details and click "Save"
DOC.ALL("ctl00_PageContent_BankChequeOrOtherRef").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 3).Value
DOC.ALL("ctl00_PageContent_PAYDRemarks").Value = ThisWorkbook.Sheets("sheet1").Cells(ROW, 4).Value
DOC.ALL("ctl00_PageContent_SaveButton__Button").Click
'Step 4&5 Ends
End Sub
发现这个代码段并没有刷新页面,即使它应该是。
If Not IE.Busy Or IE.ReadyState = 4 Then
IE.Refresh
End If
对象是否可能在中途掉落?
当关闭IE的可见性时,代码偶尔会起作用,但在弹出完全相同的错误之前,它会像每小时一次或类似的东西一样工作。
我访问的页面似乎使用ASPX并在数据库上生成页面,不知道这是否有用。
编辑:使用excel 2016在win10机器上尝试完全相同的代码。代码运行良好。