【发布时间】:2015-09-03 23:22:31
【问题描述】:
我在 VBA 中有以下代码,它打开一个 IE 页面,填充它,然后单击一个打开一个新 IE 窗口的按钮。但是,我的代码无法填写新窗口的第一个下拉菜单。任何帮助将不胜感激。
Sub Van()
Dim IE As Object
Dim IE2 As Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE2 = CreateObject("InternetExplorer.Application")
IE.navigate ("website")
IE.Visible = True
Do
DoEvents
Loop Until IE.readyState = 4
Set d = IE.document
'Code now clicks on buttons or dropdowns etc
Application.Wait (Now + TimeValue("00:00:03"))
Set IE2 = GetIE("pop-up page")
WaitFor IE2
'Tried this too
'Application.Wait (Now + TimeValue("00:00:05"))
IE2.document.getElementsByTagName("select")(0).Value = "X"
'Also tried the line below
'IE2.document.GetElementsbyname("name")(0).SelectedIndex = 1
End Sub
Function GetIE(sAddress As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim retVal As Object, sURL As String
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
'see if IE is already open
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.LocationURL
On Error GoTo 0
If sURL <> "" Then
'MsgBox sURL
If sURL = sAddress Then
Set retVal = o
Exit For
End If
End If
Next o
Set GetIE = retVal
End Function
Sub WaitFor(IE)
Do
DoEvents
Loop Until IE.readyState = 4
End Sub
【问题讨论】:
-
您传递给
GetIE的URL 中的?后面真的有这些空格吗? -
不,抱歉,这只是我错误地复制到这里的方式 - 我会编辑它