我正在用Excel数据填充字段。该子项目在其他网站上对我有用。
如何使此功能适用于弹出窗口?
我已经阅读了有关此主题的其他几页。我正在使用IE11。
这是我要填写的字段之一的示例。
<input class="form-control" id="firstName" type="text" maxlength="250" data-bind="value:firstName,disable:$parent.readOnly">
Private Sub FillWebForm_xx_AddNewEE()
Dim ie As Object
Dim HWNDSrc As Long
Dim xSheetName As String
xSheetName = "Company"
MsgBox "Open Internet Explorer and navigate to the webpage that contains the fields to be filled, then click Okay."
'Need to edit this I think: if k-window-title = "New Participant"
Set ie = GetIE("https://xxx")
'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("dateId").Value = Format$(ThisWorkbook.Sheets(xSheetName).Range("i32").Value, "mm/dd/yyyy")
ie.document.all("gender").Focus
ie.document.all("gender").Value = ThisWorkbook.Sheets(xSheetName).Range("j32").Value
'ie.document.all("gender").FireEvent ("onchange")
'Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop
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("email").Value = ThisWorkbook.Sheets(xSheetName).Range("tbd")
'ie.document.all("hireDate").Value = Format$(ThisWorkbook.Sheets(xSheetName).Range("q32").Value, "mm/dd/yyyy")
Set ie = Nothing
End Sub
这是弹出窗口的外观。
当我右键单击名字字段并单击检查元素时,这就是我看到的内容。
从您的弹出窗口图像看,它看起来像是模型弹出窗口,属于该网页而不是新窗口。
[请尝试参考下面的示例可能会帮助您打开IE的新实例并导航到特定的网页并在弹出窗口中填充数据。
Sub website()
On Error GoTo 0
Dim sURL As String
Dim oBrowser As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim oHTML_Element As IHTMLElement
sURL = "C:\Users\Administrator\Desktop\pop_up.html"
Set oBrowser = CreateObject("InternetExplorer.Application")
oBrowser.navigate sURL
Do Until oBrowser.readyState = 4
DoEvents
Loop
oBrowser.Visible = True
oBrowser.Document.getElementById("first_name").Value = "abcd added from VBA..."
'oBrowser.Document.getElementById("Login").Click
End Sub
输出:
请注意,这只是一个示例,可以给您一个想法。此外,您需要根据需要修改代码示例。
其他有用的参考资料:
((1)Automate Internet Explorer (IE) Using VBA
((2)IE (Internet Explorer) Automation using Excel VBA
((3)Learn to write Excel VBA code to automate your web browser.