使用VBA动态创建HTML表行

时间:2017-01-27 02:32:47

标签: html vba excel-vba excel

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

在下面的代码中rowstocontactCollection。我知道我想循环遍历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>"

2 个答案:

答案 0 :(得分:4)

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

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

在代码的第二部分中,您可以遍历集合(行引用)并动态构建表,方法是为<tr>...</tr>中的项目添加Collection块。

在&#39;之后设置字符串&#39;然后将所有三个部分连接在一起以获得最终的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按钮可以很好地格式化它,以提高可读性:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

HTH

答案 1 :(得分:0)

我通过将范围调整为将表格外的1个单元格调整为无格式区域来解决此问题:

        With Log_03.Range("VBA_rng_email")
            Set RNG = Union(.Resize(iRow), .Offset(.Rows.Count).Resize(1))
        End With
        strbody = strbody & RangetoHTML(RNG) & "<br>"
  • 如果要从表中获取行,iRow是数字