简体   繁体   English

vba用Excel数据填充Internet Explorer表单

[英]vba Filling Internet Explorer Forms with Excel Data

Situation: I am filling fields on a website in Internet Explorer with data from Excel. 情况:我正在用Excel中的数据填充Internet Explorer中网站上的字段。 It is an existing web page that the user has already navigated to, then clicks a button to add the Excel data to the fields. 它是用户已经导航到的现有网页,然后单击按钮以将Excel数据添加到字段中。 The code below does the job just fine on one page of the website, with the exception of the last line. 下面的代码仅在网站的一页上完成工作,最后一行除外。 The last line is a drop-down selection instead of an input box. 最后一行是下拉选择,而不是输入框。 I am not able to get this one to work. 我无法使这个工作。 Any guidance that can be given is greatly appreciated! 可以提供任何指导,我们将不胜感激!

Addtl Info: I am using IE11, I have the function and the subs, each in a separate module. 其他信息:我正在使用IE11,我具有功能和子功能,每个功能和功能都位于单独的模块中。

This one works. 这个作品。

  '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") 

This is the code that doesn't work. 这是无效的代码。 On this one, I get an error on ie.visible. 在这一点上,我在ie.visible上收到一个错误。 I've tried different variations of this but the closest I got was filling the fields, but site didn't recognize that data was entered into the fields; 我尝试了各种变体,但最接近的是填充字段,但是站点无法识别出数据已输入到字段中; it said they still needed to be populated. 它说他们仍然需要填充。 I did notice that the URL below has "control" in it. 我确实注意到下面的URL中包含“控件”。 I'm not sure if that makes a difference. 我不确定这是否有所作为。

  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 

With JB's suggestion, I got this working with .focus, .value =, .FireEvent ("onchange") 在JB的建议下,我使用了.focus,.value = 、. FireEvent(“ onchange”)

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM