有人可以帮我编辑此vba子以在弹出窗口中工作吗?我用Excel数据填充字段。这个子项目在其他网站上对我有用,但是我不知道如何使它在弹出窗口中起作用。我已经阅读了有关此主题的其他几页,但是对vba来说还是相对较新,并且没有取得任何进展。我正在使用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
这是弹出窗口的样子。
当我右键单击“名字”字段并单击“检查元素”时,这就是我所看到的。
答案 0 :(得分:0)
从弹出窗口的图像中,它看起来像是模型弹出窗口,属于该网页而不是新窗口。
请尝试参考下面的示例,它可以帮助您打开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.