情况:我正在用Excel中的数据填充Internet Explorer中网站上的字段。它是用户已经导航到的现有网页,然后单击按钮以将Excel数据添加到字段中。下面的代码仅在网站的一页上完成工作,最后一行除外。最后一行是下拉选择,而不是输入框。我无法使这个工作。可以提供任何指导,我们将不胜感激!
附加信息:我正在使用IE11,我具有功能和子功能,它们分别位于单独的模块中。
这个作品。
'This Must go at the top of your module. It's used to set IE as the active window Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As LongPtr Function GetIE(sLocation As String) As Object Dim objShell As Object, objShellWindows As Object, o As Object Dim sURL As String Dim retVal As Object Set retVal = Nothing Set objShell = CreateObject("Shell.Application") Set objShellWindows = objShell.Windows For Each o In objShellWindows sURL = "" On Error Resume Next 'because may not have a "document" property 'Check the URL and if it's the one you want then ' assign the window object to the return value and exit the loop sURL = o.document.Location On Error GoTo 0 If sURL Like sLocation & "*" Then Set retVal = o Exit For End If Next o Set GetIE = retVal End Function Option Explicit Private Sub FillWebForm_FLBlue_AddNewEE() Dim objIE As Object Dim ie As Object Dim HWNDSrc As Long Dim xSheetName As String xSheetName = "FloridaBlue" MsgBox "Open Internet Explorer and navigate to the webpage that contains the fields to be filled, then click Okay." 'Look for a specific URL in an existing instance of Internet Explorer. Set ie = GetIE("https://secure2.benefitfocus.com/hradmin/task/enrollment/sponsor/employee/CreateEmployee/") 'make browser visible (if existing instance of IE) ie.Visible = True 'Get Window ID for IE so we can set it as activate window HWNDSrc = ie.hwnd 'Set IE as Active Window SetForegroundWindow HWNDSrc 'Add a new employee ie.document.all("SSN").Value = ThisWorkbook.Sheets(xSheetName).Range("d32") ie.document.all("firstName").Value = ThisWorkbook.Sheets(xSheetName).Range("e32") ie.document.all("lastName").Value = ThisWorkbook.Sheets(xSheetName).Range("g32") ie.document.all("suffix").Value = ThisWorkbook.Sheets(xSheetName).Range("h32") ie.document.all("birthDate").Value = Format$(ThisWorkbook.Sheets(xSheetName).Range("i32").Value,"mm/dd/yyyy") ie.document.all("gender").Value = ThisWorkbook.Sheets(xSheetName).Range("j32") ie.document.all("address1").Value = ThisWorkbook.Sheets(xSheetName).Range("k32") ie.document.all("address2").Value = ThisWorkbook.Sheets(xSheetName).Range("l32") ie.document.all("city").Value = ThisWorkbook.Sheets(xSheetName).Range("m32") ie.document.all("state").Value = ThisWorkbook.Sheets(xSheetName).Range("n32") ie.document.all("zip").Value = ThisWorkbook.Sheets(xSheetName).Range("o32") ie.document.all("country").Value = ThisWorkbook.Sheets(xSheetName).Range("p32") ie.document.all("hireDate").Value = Format$(ThisWorkbook.Sheets(xSheetName).Range("q32").Value,"mm/dd/yyyy") IE.document.all("categorySelections").Focus IE.document.all("categorySelections").Value = ThisWorkbook.Sheets("Sheet1").Range("r32")
这是无效的代码。在这一点上,我在ie.visible上收到一个错误。我尝试了各种变体,但最接近的是填充字段,但是站点无法识别出数据已输入到字段中;它说他们仍然需要填充。我确实注意到下面的URL中包含“控件”。我不确定这是否有所作为。
Private Sub FillWebForm_FLBlue_AddNewDep() Dim ie As Object Dim HWNDSrc As Long Dim xSheetName As String xSheetName = "FloridaBlue" 'Look for a specific URL in an existing instance of Internet Explorer. Set ie = GetIE("https://secure2.benefitfocus.com/hradmin/control/dependentBeneficiaryListAction#dependent/new") 'make browser visible (if existing instance of IE) ie.Visible = True 'The error occurs here. Object not found. 'Get Window ID for IE so we can set it as activate window HWNDSrc = ie.hwnd 'Set IE as Active Window SetForegroundWindow HWNDSrc ie.document.all("rawSsn").Value = ThisWorkbook.Sheets(xSheetName).Range("d44") ie.document.all("firstName").Value = ThisWorkbook.Sheets(xSheetName).Range("e44") ie.document.all("lastName").Value = ThisWorkbook.Sheets(xSheetName).Range("g44") ie.document.all("suffix").Value = ThisWorkbook.Sheets(xSheetName).Range("h44") ie.document.all("dob-alt").Value = Format$(ThisWorkbook.Sheets(xSheetName).Range("i44").Value,"mm/dd/yyyy") ie.document.all("gender").Value = ThisWorkbook.Sheets(xSheetName).Range("j44") ie.document.all("relationship").Value = ThisWorkbook.Sheets(xSheetName).Range("q44").Value
答案 0 :(得分:0)
在J.B.的建议下,我使用了.focus,.value = 、. FireEvent(“ onchange”)
谢谢!