使用HTML表将数据从excel检索到outlook

时间:2015-05-18 12:20:24

标签: html excel vba excel-vba outlook

这里的VBA相当新。我正在尝试编写一段VBA,它将从已经打开的excel文件中复制一些数据并将其粘贴到HTML格式的转发电子邮件中。创建电子邮件工作正常,问题是从excel检索数据时似乎陷入无休止的循环。它被卡在str = str&线上。 “”& c.Value& “” 它将通过第一个用于r循环清除,但随后在第二个循环中重复停留。

以下是代码:

Function GetExcelData() As String

    Dim OrderColumn As Range, OrderRow As Range, r As Range, c As Range
    Dim str As String
    Dim xl As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    'Dim rn As Excel.Range

    Set xl = GetObject(, "Excel.Application")
    Set wb = xl.Workbooks("Book1")
    Set ws = wb.Sheets(1)

    ws.Activate
    Set OrderColumn = ws.Range("A1", ws.Range("A1").End(xlDown))

    str = "<table>"

    For Each r In OrderColumn
        str = str & "<tr>"

        Set OrderRow = ws.Range(r, r.End(xlToRight))

        For Each c In OrderRow

            str = str & "<td>" & c.Value & "</td>"

        Next c

        str = str & "</tr>"
    Next r
    str = str & "</table>"
    GetExcelData = str    
End Function

任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

您可以考虑使用Word对象模型来完成工作,而不是为Excel范围编写HTML标记。 Inspector类的WordEditor属性从Word对象模型返回Document类的实例。有关详细信息,请参阅Chapter 17: Working with Item Bodies