【发布时间】:2015-01-26 16:26:27
【问题描述】:
我正在尝试从 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
【问题讨论】:
-
尝试将
DistArrayElements设置为-1。如果您希望数组从 0 开始,请确保以Option Base 0开始您的宏。 -
如果我这样做,我得到,索引超出了数组的范围。它不必从零开始......
-
试试这个
assetListBox.Items.Count-1我认为计数是从 1 开始的。 -
现在它在第一个分发列表上等于 7,这是正确的,我还有 2 个有 10 个和 9 个成员。这些都不正确。
-
好的,这行得通,我将 IF 语句移到了循环之外。在上面编辑。