我试图将数据传递到第四页上的表单。我已经成功浏览了宏,并根据输入的数据搜索特定页面,转到该特定页面,然后将我想要输入数据的表单拉到多个位置。
Sub Test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://mywebsite.com/Pages/MainSite/Default.aspx"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementById("txtUserName").Value = "dummyusername"
IE.document.getElementById("txtPassword").Value = "password"
IE.document.getElementById("btnLogin").Click
End Sub
Sub Next2()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "mywebsite.com/pages/hr/TimeEntryDashboard.aspx"
Do While IE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
IE.document.getElementById("MainContent_MainContent_txtStartDateFilter").Value = "10/13/2018"
IE.document.getElementById("MainContent_MainContent_txtEmployeeNameFilter").Value = "122631"
IE.document.getElementById("MainContent_MainContent_btnFind").Click
End Sub
Sub Next3()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "mywebsite.com/pages/hr/TimeAndInspectionWizard.aspx?id=0245b750-4cde-47da-a754-fb7f8bfecfc9"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementById("imgGridTimeDetailsArrow0").Click
IE.document.getElementById("MainContent_MainContent_tcMain_tpValidation_rptInspectionDetails_imbGridInspectionDetailsOptionsAdd_0").Click
End Sub
Sub Next4()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
' IE.Visible = True
' IE.Navigate "mywebsite.com/pages/hr/TimeAndInspectionWizard.aspx?id=0245b750-4cde-47da-a754-fb7f8bfecfc9"
Do While IE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionDetailsLotNumber").Value = "12345"
IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionInspectedQuantity").Value = "1"
IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtGoodWithoutReworkQuantity").Value = "1"
IE.document.getElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult").Click
End Sub
上面这部分是在第一部分产生错误,它应该在屏幕上输入数据。
您必须检查元素是否存在作为代码验证的一部分,这也将导致您以更好的方式识别问题。以下是您使用验证重写的代码。
Sub Next4()
Dim IE As Object, Elem As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
Do While .Busy
Application.Wait DateAdd("s", 2, Now)
Loop
With .document
Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionDetailsLotNumber")
If Not Elem is nothing Then
Elem.Value = "12345"
Set Elem = Nothing
End If
Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtInspectionInspectedQuantity")
If Not Elem is nothing Then
Elem.Value = "1"
Set Elem = Nothing
End If
Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtGoodWithoutReworkQuantity")
If Not Elem is nothing Then
Elem.Value = "1"
Set Elem = Nothing
End If
Set Elem = .GetElementById("MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult")
If Not Elem is nothing Then
Elem.Click
Else
MsgBox "Can't Click as Element Missing : " & "MainContent_MainContent_tcMain_tpInspectionDetails_txtAddInspectionResult"
End If
End With
End With
End Sub
您尚未说明错误或显示任何HTML。可能是您的元素位于父框架/ iframe中。
While .Busy Or .readyState < 4: DoEvents: Wend
。如果你加入你的潜水员,你将需要在每个.Click/.Submit/.Navigate
之后On Error Resume Next
中的对象引用以保持代码运行,并防止用户在未设置元素时看到错误消息。VBA:
Option Explicit
Public Sub LoopUntilSet()
Dim IE As New InternetExplorer, t As Date
Const MAX_WAIT_SEC As Long = 5
With IE
.Visible = True
.navigate "yourURL"
While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer
Do While x Is Nothing
DoEvents
On Error Resume Next
Set ele = .document.getElementById("txtUserName")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop
If ele Is Nothing Then
Exit Sub
Else
ele.Value = "dummyusername"
'other code
End If
.Quit
End With
End Sub