我正在尝试从DL中获取AD中的成员,并将这些成员添加到数组中,同时还将它们显示在列表框中。我的问题是数组说数组中有8个元素,但是列表框显示为7.因此当从列表框中的0开始计数时,应该说6,而不是8.此外,当objGroup.Members更改到期时对于我正在返回的distGroup,当数组应该更多时,数组也保持在8。谁能帮我?我不是VB大师。
Dim distGroup As String
Dim asset As String
Dim distArray() As String
Dim distArrayElements As Integer = 0
Sub getMembers()
Dim objGroup, objUser, objFSO, strDomain
assetListBox.Items.Clear()
If distGroup = Nothing Then
Exit Sub
End If
'Change DomainName to the name of the domain the group is in
strDomain = "Mydomain.com"
objGroup = GetObject("LDAP://CN=" & distGroup & ",OU=Loaners,OU=Accounts,DC=myDomain,DC=com")
For Each objUser In objGroup.Members
assetListBox.Items.Insert(0, objUser.displayName)
assetListBox.Sorted = True
asset = assetListBox.Items.Item(0)
If distArrayElements > assetListBox.Items.Count Then
distArrayElements = Nothing
End If
Next
Dim i As Integer
For i = 0 To assetListBox.Items.Count
AddElementToStringArray("assetListBox.Items " & i)
Next
MsgBox("Number of elements in array is: " & distArray.Length & vbCrLf & vbCrLf & "Asset: " & asset)
End Sub
Public Sub AddElementToStringArray(ByVal stringToAdd As String)
ReDim Preserve distArray(distArrayElements)
distArray(distArrayElements) = stringToAdd
distArrayElements += 1
End Sub
答案 0 :(得分:1)
建议减去1 assetListBox.Items.Count
Dim distGroup As String
Dim asset As String
Dim distArray() As String
Dim distArrayElements As Integer = 0
Sub getMembers()
Dim objGroup, objUser, objFSO, strDomain
assetListBox.Items.Clear()
If distGroup = Nothing Then
Exit Sub
End If
'Change DomainName to the name of the domain the group is in
strDomain = "Mydomain.com"
objGroup = GetObject("LDAP://CN=" & distGroup & ",OU=Loaners,OU=Accounts,DC=myDomain,DC=com")
For Each objUser In objGroup.Members
assetListBox.Items.Insert(0, objUser.displayName)
assetListBox.Sorted = True
asset = assetListBox.Items.Item(0)
If distArrayElements > assetListBox.Items.Count Then
distArrayElements = Nothing
End If
Next
Dim i As Integer
For i = 0 To assetListBox.Items.Count - 1
AddElementToStringArray("assetListBox.Items " & i)
Next
MsgBox("Number of elements in array is: " & distArray.Length & vbCrLf & vbCrLf & "Asset: " & asset)
End Sub
Public Sub AddElementToStringArray(ByVal stringToAdd As String)
ReDim Preserve distArray(distArrayElements)
distArray(distArrayElements) = stringToAdd
distArrayElements += 1
End Sub