【发布时间】:2019-02-25 10:40:42
【问题描述】:
情况:我正在 Internet Explorer 中使用 Excel 中的数据填充网站上的字段。这是用户已经导航到的现有网页,然后单击按钮将 Excel 数据添加到字段中。下面的代码在网站的一个页面上可以很好地完成工作,除了最后一行。最后一行是下拉选择而不是输入框。我无法让这个工作。非常感谢任何可以提供的指导!
Addtl Info:我使用的是 IE11,我有函数和 subs,每个都在一个单独的模块中。
这个有效。
'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
【问题讨论】:
-
哪一行设置下拉列表的值?
-
我不确定您所说的字段,但是一旦设置了值,您需要添加一行 IE.document.all("categorySelections").FireEvent ("onchange") 。
-
您在上面至少描述了两个问题。真正的问题是什么?
-
我将尝试将 FireEvent("onchange") 添加到第一个代码中。在第二个代码中,问题是我在 ie.visible 的行上遇到错误,但它与上面的代码相同,不会产生错误。
-
@J.B.我添加了 IE.document.all("categorySelections").FireEvent ("onchange") 并且运行时错误脚本超出范围。我在添加值之前和之后都尝试过这个/我还尝试了 .innertext = 和 .value=,并且两者都得到了相同的错误。
标签: javascript excel vba internet-explorer internet-explorer-11