Ситуация: я заполняю поля на веб-сайте в Internet Explorer данными из Excel.Это существующая веб-страница, к которой пользователь уже перешел, затем она нажимает кнопку, чтобы добавить данные 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