Excel VBA getElementsByTagName()仅返回最后一个输入 [英] Excel VBA getElementsByTagName() only returning the last input
问题描述
我的公司需要为1000多个用户重置用户名和密码,而不是单枪匹马地进行操作,我希望VBA可以自动为我执行此操作.我已经很好地将其连接到IE窗口,但getElementsByTagName("input")仅返回最后一个输入标签.下面是我的代码:
My company is needing to reset usernames and passwords for over 1000 people and instead of doing it emanually, i would like to have VBA automate it for me. I have it connect to the IE window fine but the getElementsByTagName("input") only returns the last input tag. Below is my code:
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
我想要的输入标签在表格标签中,这可能会导致它吗?如果是这样,我将如何引用表格中的标签?
The input tags that i am wanting are in table tags, could that be causing it? If so, how would i reference the tags inside the table?
谢谢.
推荐答案
尝试以下循环:
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
不过不太确定:您似乎已经从问题中省略了一些代码.
Not quite sure though: you seem to have omitted some code from your question.
这篇关于Excel VBA getElementsByTagName()仅返回最后一个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!