html - 使用 VBA 动态创建 HTML 表格行

标签 html vba excel

我正在使用 VBA 在 Outlook 电子邮件中创建一个表。我已经弄清楚如何生成表,但我的问题是我需要动态调整表中的行数。对于某些电子邮件,将有两行数据,对于其他电子邮件,将有三行,依此类推。

在下面的代码中,rowstocontact 是一个Collection。我知道我想循环遍历 Collection 并为集合中的每个项目添加一行,但我不知道如何在创建表的 html 代码中插入循环。

非常感谢任何帮助!谢谢。

    bodytext = "<head><style>table, th, td {border: 1px solid gray; border-collapse:" & _
    "collapse;}</style></head><body>" & _
    "<table style=""width:60%""><tr>" & _
    "<th bgcolor=""#bdf0ff"">Reviewee</th>" & _
    "<th bgcolor=""#bdf0ff"">Manager(s)</th>" & _
    "<th bgcolor=""#bdf0ff"">Project code</th>" & _
    "<th bgcolor=""#bdf0ff"">Requested</th>" & _
    "<th bgcolor=""#bdf0ff"">Type</th>" & _
    "<th bgcolor=""#bdf0ff"">Due</th></tr><tr>" & _
    "<td ""col width=10%"">" & Range("D" & rowtocontact(1)) & "</td>" & _
    "<td ""col width=10%"">" & Range("L" & rowtocontact(1)) & "</td>" & _
    "<td ""col width=10%"">" & Range("M" & rowtocontact(1)) & "</td>" & _
    "<td ""col width=10%"">" & Range("AJ" & rowtocontact(1)) & "</td>" & _
    "<td ""col width=10%"">" & Range("V" & rowtocontact(1)) & "</td>" & _
    "<td ""col width=10%"">" & Range("AK" & rowtocontact(1)) & "</td>" & _
    "<td ""col width=10%"">" & Range("AK" & rowtocontact(1)) & "</td>" & _
    "</tr></Table></body>"

最佳答案

您需要将 HTML 分成 3 部分,并为每个部分设置一个字符串变量:

  • 行之前的所有内容
  • 行之后的所有内容

在代码的第二部分中,您可以迭代(行引用)集合并通过添加 <tr>...</tr> 动态构建表。 block 的数量与您的 Collection 中的数量一样多。 .

设置“行后所有内容”的字符串后,您可以将所有三个部分连接在一起以获得最终的 HTML 字符串。

这是示例代码 - 请注意,我添加了工作表引用 ( ws ) 作为最佳实践,并且还放弃了最终的 <td>每行,因为它看起来像是列 AK 上的值的重复项您没有标题。无论如何,您都可以根据需要进行调整:

Option Explicit

Sub CreateEmailHtml()

    Dim ws As Worksheet
    Dim coll As New Collection
    Dim lngCounter As Long
    Dim strBeforeRows As String
    Dim strRows As String
    Dim strAfterRows As String
    Dim strAll As String

    ' get a worksheet reference
    Set ws = Sheet1

    ' test collection
    coll.Add 2
    coll.Add 4
    coll.Add 6

    ' HTML before rows
    strBeforeRows = "<head><style>table, th, td {border: 1px solid gray; border-collapse:" & _
        "collapse;}</style></head><body>" & _
        "<table style=""width:60%""><tr>" & _
        "<th bgcolor=""#bdf0ff"">Reviewee</th>" & _
        "<th bgcolor=""#bdf0ff"">Manager(s)</th>" & _
        "<th bgcolor=""#bdf0ff"">Project code</th>" & _
        "<th bgcolor=""#bdf0ff"">Requested</th>" & _
        "<th bgcolor=""#bdf0ff"">Type</th>" & _
        "<th bgcolor=""#bdf0ff"">Due</th></tr>"

    ' iterate collection
    strRows = ""
    For lngCounter = 1 To coll.Count
        strRows = strRows & "<tr>"
        strRows = strRows & "<td ""col width=10%"">" & ws.Range("D" & coll(lngCounter)).Value & "</td>"
        strRows = strRows & "<td ""col width=10%"">" & ws.Range("L" & coll(lngCounter)).Value & "</td>"
        strRows = strRows & "<td ""col width=10%"">" & ws.Range("M" & coll(lngCounter)).Value & "</td>"
        strRows = strRows & "<td ""col width=10%"">" & ws.Range("AJ" & coll(lngCounter)).Value & "</td>"
        strRows = strRows & "<td ""col width=10%"">" & ws.Range("V" & coll(lngCounter)).Value & "</td>"
        strRows = strRows & "<td ""col width=10%"">" & ws.Range("AK" & coll(lngCounter)).Value & "</td>"
        strRows = strRows & "</tr>"
    Next lngCounter

    ' HTML after rows
    strAfterRows = "</table></body>"

    ' final HTML - concatenate the 3 string variables
    strAll = strBeforeRows & strRows & strAfterRows

    Debug.Print strAll

End Sub

因此,鉴于此示例数据:

enter image description here

您得到这个 HTML 作为输出 - 它通过使用 Tidy 进行了很好的格式化。堆栈片段编辑器中的按钮以提高可读性:

<head>
  <style>
    table,
    th,
    td {
      border: 1px solid gray;
      border-collapse: collapse;
    }
  </style>
</head>

<body>
  <table style="width:60%">
    <tr>
      <th bgcolor="#bdf0ff">Reviewee</th>
      <th bgcolor="#bdf0ff">Manager(s)</th>
      <th bgcolor="#bdf0ff">Project code</th>
      <th bgcolor="#bdf0ff">Requested</th>
      <th bgcolor="#bdf0ff">Type</th>
      <th bgcolor="#bdf0ff">Due</th>
    </tr>
    <tr>
      <td "col width=10%">foo2</td>
      <td "col width=10%">bar2</td>
      <td "col width=10%">baz2</td>
      <td "col width=10%">quux2</td>
      <td "col width=10%">qux2</td>
      <td "col width=10%">quuux2</td>
    </tr>
    <tr>
      <td "col width=10%">foo2</td>
      <td "col width=10%">bar2</td>
      <td "col width=10%">baz2</td>
      <td "col width=10%">quux2</td>
      <td "col width=10%">qux2</td>
      <td "col width=10%">quuux2</td>
    </tr>
    <tr>
      <td "col width=10%">foo6</td>
      <td "col width=10%">bar6</td>
      <td "col width=10%">baz6</td>
      <td "col width=10%">quux6</td>
      <td "col width=10%">qux6</td>
      <td "col width=10%">quuux6</td>
    </tr>
  </table>
</body>

HTH

关于html - 使用 VBA 动态创建 HTML 表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41886251/

相关文章:

arrays - Excel SUMIF,其中标准在数组中

c# - 我可以通过剪贴板将 [Serializable] 对象传递到 Excel 加载项吗?

html - 单击此 div 时隐藏附近 div 的 css(用于悬停)

java - index.html 和 index.jsp 有什么区别?

sql-server - 从 vba 调用 MS SQL Server 标量 UDF

sql-server - ADODB受影响的行返回触发器受影响的行

vb.net - 代码无响应

html - CSS 子选择器 (>) 不适用于某些属性

javascript - 获取 Canvas 鼠标在 Canvas 上的位置

excel - 如何遍历单元格列并写入另一列单元格