2017-01-27 3 views
2

Ich verwende VBA, um eine Tabelle in einer Outlook-E-Mail zu erstellen. Ich habe herausgefunden, wie man die Tabelle generiert, aber mein Problem ist, dass ich die Anzahl der Zeilen in der Tabelle dynamisch anpassen muss. Für einige E-Mails gibt es zwei Datenzeilen, für andere drei, usw.HTML-Tabellenzeilen dynamisch mit VBA erstellen

Im folgenden Code ist rowstocontact ein Collection. Ich weiß, dass ich die Collection Schleife durchlaufen und eine Zeile für jedes Element in der Auflistung hinzufügen möchte, aber ich kann nicht herausfinden, wie Sie eine Schleife in dem HTML-Code einfügen, der die Tabelle erstellt.

Jede Hilfe wird sehr geschätzt !! Vielen Dank.

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>" 
+0

Robin Mackenzie - können Sie mit meiner Post helfen? https://stackoverflow.com/questions/47360932/create-html-table-with-excel-data-dynamic-rows – Jono

Antwort

3

Sie müssen den HTML-Code in drei Teile aufzuspalten und eine String-Variable für jeweils:

  • Alles, bevor die Zeilen
  • Die Reihen
  • Alles nach den Reihen

Im zweiten Codeabschnitt können Sie die Auflistung (von Zeilenreferenzen) durchlaufen und die Tabelle dynamisch erstellen durch Hinzufügen von <tr>...</tr> Blöcke für so viele Gegenstände wie es in Ihrem Collection sind.

Nach dem Setzen der Zeichenfolge für "alles nach den Zeilen" verketten Sie dann alle drei Teile zusammen, um die endgültige HTML-Zeichenfolge zu erhalten. Hier

ist der Beispielcode - stelle ich ein Arbeitsblatt Referenz (ws) als Best Practice hinzugefügt und auch die endgültige <td> pro Zeile abgesetzt, da es wie ein Duplikat der Wert auf Spalte schien AK, für die Sie nicht haben, eine Überschrift. Sie können trotzdem als Anzüge einstellen:

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 

gegeben Also, diese Beispieldaten:

enter image description here

Sie HTML als Ausgabe erhalten - es schön durch die Verwendung der Tidy Taste in der formatiert ist Editor zur besseren Lesbarkeit Stack-Schnipsel:

<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