列格式的VBS输出

时间:2014-07-30 18:35:13

标签: vbscript

我正在尝试将我的.vbs输出放入列中。但是,当我尝试编写代码以将输出组织成列时,我继续收到错误 - 无效的过程调用或参数:'Space'

我正在寻求一些帮助,谢谢!!

Call FindPCsThatUserLoggedInto

Sub FindPCsThatUserLoggedInto()

strUser = InputBox("Enter First Name")
strLast = InputBox("Enter Last Name")

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

's = "Name" & Chr(9) & "Account Name"  & Chr(9) &  "Location" & Chr(10) & Chr(13)
's = s & "----" & Chr(9) & "------------"  & Chr(9) &  "--------" & Chr(10) & Chr(13)
s = RightJustified("Name", 10) & _
  RightJustified("Account Name", 15) & _
  RightJustified("Location", 15) & _
  vbCrLf

objCommand.CommandText = "SELECT ADSPath FROM 'LDAP://dc=hc,dc=company,dc=com' WHERE givenName = '" & strUser & "*' AND sn = '" & strLast & "*'"

Set objRecordSet = objCommand.Execute

If objRecordSet.Recordcount > 0 Then

    objRecordSet.MoveFirst

    Do Until objRecordSet.EOF

        Set objUser = GetObject(objRecordSet.Fields("ADSPath").Value)

        's = s & objUser.DisplayName & Chr(9) & objUser.samaccountname  & Chr(9) &  objUser.PhysicalDeliveryOfficeName & Chr(10) & Chr(13)

       ' objRecordSet.MoveNext
       s = s & _
  RightJustified(objUser.DisplayName, 10) & _
  RightJustified(objUser.samaccountname, 15) & _
  RightJustified(objUser.PhysicalDeliveryOfficeName, 15) & _
  vbCrLf

    Loop

    MsgBox s

Else

    MsgBox "No users matching that criteria exist in the HC domain in AD."

End If

End Sub

Function RightJustified(ColumnValue, ColumnWidth)
RightJustified = Space(ColumnWidth - Len(ColumnValue)) & ColumnValue
End Function

这是我为将输出组织成列而添加的代码:

s = RightJustified("Name", 10) & _
  RightJustified("Account Name", 15) & _
  RightJustified("Location", 15) & _
  vbCrLf

       s = s & _
  RightJustified(objUser.DisplayName, 10) & _
  RightJustified(objUser.samaccountname, 15) & _
  RightJustified(objUser.PhysicalDeliveryOfficeName, 15) & _
  vbCrLf

这是我的输出: enter image description here

1 个答案:

答案 0 :(得分:0)

我的猜测是,由于您的数据超出了指定的大小,这是一个运行时错误?

RightJustified(objUser.DisplayName, 10) & _
RightJustified(objUser.samaccountname, 15) & _
RightJustified(objUser.PhysicalDeliveryOfficeName, 15) & _
vbCrLf

也许需要做出这样的改变:

Function RightJustified(ColumnValue, ColumnWidth)
  If Len(ColumnValue) > ColumnWidth Then
     ColumnValue = Left(ColumnValue,ColumnWidth)
  End If
  RightJustified = Space(ColumnWidth - Len(ColumnValue)) & ColumnValue
End Function