我的公司需要为超过1000人重置用户名和密码而不是通过手工操作,我希望让VBA为我自动化。我把它连接到IE窗口很好,但是getElementsByTagName(“input”)只返回最后一个输入标签。以下是我的代码:
Dim shellWins As ShellWindows
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim objElement As Object
Dim objCollection As IHTMLElementCollection
Dim name As String
Dim val As String, val2 As String
Dim a
Set shellWins = New ShellWindows
If shellWins.Count > 0 Then
' Get IE
Set IE = shellWins.Item(0)
Else
' Create IE
Set IE = New InternetExplorer
IE.Visible = True
End If
Set objCollection = IE.Document.getElementsByTagName("input")
For i = 0 To objCollection.Length
name = objCollection(, i).name
If Left(name, 6) = username Then
val = objCollection(i).Value
a = Split(val, "@")
If Left(a(1), 1) <> "s" Then
val2 = a(0) & sa & a(1)
Else
val2 = val
End If
objCollection(i).Value = val2
ElseIf Left(name, 6) = pswd Then
objCollection(i).Value = nPswd
End If
Next
Set shellWins = Nothing
Set IE = Nothing
我想要的输入标签是否在表标签中,是否可能导致它?如果是这样,我将如何引用表格内的标签?
提前谢谢。
答案 0 :(得分:1)
尝试使用此循环:
Dim el as object
Set objCollection = IE.document.getElementsByTagName("input")
For Each el In objCollection
If Left(el.Name, 6) = UserName Then '?"username"?
a = Split(el.Value, "@")
If Left(a(1), 1) <> "s" Then
val2 = a(0) & sa & a(1)
el.Value = val2
End If
ElseIf Left(el.Name, 6) = pswd Then
el.Value = nPswd
End If
Next el
不太确定:你似乎已经省略了你问题中的一些代码。
答案 1 :(得分:-1)
另一种方法是使用查询从网页中检索数据。例如,下面的代码从站点http://finance.yahoo.com/q?s=usdCAd=x检索数据并将其放在单元格A1中:
With Sheet1.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q?s=usdCAd=x", Destination:=Sheet1.Range("$A$1"))
.Name = "q?s=usdCAd=x_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
您最终将从网站上检索大量行和一些字符串列。然后,您可以处理信息并检索所需的数据。