如何格式化InternetExplorer对象中的表?

时间:2014-11-05 14:53:53

标签: html css vbscript

我正在寻找一些关于我编写的这个脚本的输出格式的帮助。该脚本有效,但我的问题是关于输出结果。

使用此脚本,我可以从AD抓取数据并以objIE形式输出结果。我这样做了所以信息看起来很整洁。我的问题是如何将HTML编码为VBScript(非VBS编码为HTML)。下面是一张结果如何的图片,但我不确定如何进行样式编辑(如下图所示)。

这是我的代码:

Call FindPCsThatUserLoggedInto

Sub FindPCsThatUserLoggedInto()
  'Get name to search for
  strUser = InputBox("Please Enter User's First Name")

  If strUser <> "" Then
    strLast = InputBox("Please Enter User's Last Name")

    If strLast <> "" Then
      'Set location parameter
      strLocation = ("Location")

      'Set AD Constant
      Const ADS_SCOPE_SUBTREE = 2

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

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

      'Set Active Directory connection object
      Set objCommand.ActiveConnection = objConnection

      ' Set AD Command properties
      objCommand.Properties("Page Size") = 1000
      objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

      'Begin building HTML string headers
      s = "<table style = ""width:100%"" border = ""1""><tr><th>Name:</th><th>Username:</th><th>Location:</th><th>Employee ID:</th><th>Contractor ID:</th><th>Badge ID:</th></tr>"

      'Issue AD command
      objCommand.CommandText = "SELECT ADSPath FROM 'LDAP://dc=,dc=,dc=com' WHERE givenName = '" & strUser & "*' AND sn = '" & strLast & "*' And physicalDeliveryOfficeName = '" & strLocation & "'"

      'Set RecordSet object to results
      Set objRecordSet = objCommand.Execute

      'Make sure there are records returned
      If objRecordSet.Recordcount > 0 Then
        'Point to first record
        objRecordSet.MoveFirst

        'Loop through all records
        Do Until objRecordSet.EOF
          'Set user object
          Set objUser = GetObject(objRecordSet.Fields("ADSPath").Value)

          'Create temporary user string of the user name reversed twice
          strUser = strReverse(objUser.samaccountname) & strReverse(objUser.samaccountname)

          'Create temporary badge # reversed twice
          strBadge = strReverse(objUser.BadgeID) & strReverse(objUser.BadgeID)

          'Create TEMPID interlacing the first 5 characters of the two temp strings
          strTEMPID = MID(strUser,1,1)&MID(strBadge,1,1)&MID(strUser,2,1)&MID(strBadge,2,1)&MID(strUser,3,1)&MID(strBadge,3,1)&MID(strUser,4,1)&MID(strBadge,4,1)&MID(strUser,5,1)&MID(strBadge,5,1)

          'Populate HTML table with results
          s = s & "<tr><td>" & objUser.DisplayName & "</td><td>" & objUser.samaccountname & "</td><td>" & objUser.physicalDeliveryOfficeName & "</td><td>" & objUser.EmployeeNumber & "</td><td>" & objUser.ContractorID & "</td><td>" & objUser.BadgeID & "</td><td>" 

          'Move to next record
          objRecordSet.MoveNext
        Loop

        'Finish HTML string
        s = s & "</table>"

        'Create IE object and assign our HTML string to the body
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.Navigate("about:blank")
        objIE.ToolBar = 0
        objIE.StatusBar = 0
        Set objDoc = objIE.Document.Body
        objDoc.InnerHTML = s
        objIE.Visible = True
      Else
        'Inform user of no records
        MsgBox "No users matching that criteria exist in AD."
      End If
    End If
  End If
End Sub

结果如下:

actual format

以下是我希望输出的样子:

desired format

我知道用于创建此代码的HTML代码 - 我只是不确定将HTML代码放在VBScript中的哪个位置以使其输出。

1 个答案:

答案 0 :(得分:1)

您通常在HTML中通过CSS进行此类格式化。使用InternetExplorer.Application个对象,您可以定义如下样式表:

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
...

Set css = objIE.Document.CreateStyleSheet
css.AddRule "table", "border: 1px solid lightgray; border-collapse: collapse;"
css.AddRule "th", "color: black; background-color: white; font-weight: bold;"
css.AddRule "th, td", "border: 1px solid lightgray;"
css.AddRule "tr:nth-child(odd)", "background-color: lightgray;"
css.AddRule "tr:nth-child(even)", "background-color: white;"

...

:nth-child伪类允许您在偶数行和奇数行之间交替行颜色。