如何在VBA中从文本创建一个表格

3
以下代码从Excel文件中提取数据,并通过电子邮件地址将所有数据合并并发送到相应的电子邮件地址。它可以正常工作,但我希望让数据看起来更好。有没有一种方法可以将以下信息制成表格?
我希望电子邮件具有以下标题:
|_____|_____|_____|_____|
|_____|_____|_____|_____|

我看到过针对OFT文件的临时表,但是在Excel中没有直接使用。以下是相关代码,但是我不确定如何在这段代码中实现相同效果:

tmpTbl = tmpTbl & "<tr><td></td><td></td><td align=""center"">*Company</td></tr></table>"

Option Explicit

Sub Consolidate()

    #If Early Then
        Dim emailInformation As New Scripting.Dictionary
    #Else
        Dim emailInformation As Object
        Set emailInformation = CreateObject("Scripting.Dictionary")
    #End If

    GetEmailInformation emailInformation
    SendInfoEmail emailInformation

End Sub

Sub GetEmailInformation(emailInformation As Object)

    Dim rg As Range
    Dim sngRow As Range
    Dim emailAddress As String
    Dim myAppInfo As AppInfo
    Dim AppInfos As Collection

    Set rg = Range("A1").CurrentRegion           ' Assuming the list starts in A1 and DOES NOT contain empty row
    Set rg = rg.Offset(1).Resize(rg.Rows.Count - 1) ' Cut the headings

    For Each sngRow In rg.Rows

        emailAddress = sngRow.Cells(1, 1)

        Set myAppInfo = New AppInfo
        With myAppInfo
            .app = sngRow.Cells(1, 2)            'code
            .version = sngRow.Cells(1, 3)        'Company Name
            .ticker = sngRow.Cells(1, 4)         'Abbreviation
            .group = sngRow.Cells(1, 5)          'group sub group
            .lead = sngRow.Cells(1, 6)           'leader
            .banker = sngRow.Cells(1, 7)         'bank
            .analyst = sngRow.Cells(1, 8)        'analyst
            .otw = sngRow.Cells(1, 9)            'at
            .rating = sngRow.Cells(1, 10)        'rank
            .watchlist = sngRow.Cells(1, 11)     'Comments
            .legal = sngRow.Cells(1, 12)         'notes
            .add = sngRow.Cells(1, 13)           'Date
            .last = sngRow.Cells(1, 14)          'Updated
            .id = sngRow.Cells(1, 15)            'ID
        End With

        If emailInformation.Exists(emailAddress) Then
            emailInformation.item(emailAddress).add myAppInfo
        Else
            Set AppInfos = New Collection
            AppInfos.add myAppInfo
            emailInformation.add emailAddress, AppInfos
        End If

    Next

End Sub

Sub SendInfoEmail(emailInformation As Object)

    Dim sBody As String
    Dim sBodyStart As String
    Dim sBodyInfo As String
    Dim sBodyEnd As String
    Dim emailAdress As Variant
    Dim colLines As Collection
    Dim line As Variant

    sBodyStart = "Hi, please find your info below:" & vbCrLf & vbCrLf

    For Each emailAdress In emailInformation
        Set colLines = emailInformation(emailAdress)
        sBodyInfo = ""
        For Each line In colLines
            sBodyInfo = sBodyInfo & _
                        "Code: " & line.app & vbTab & "Company Name:   " & line.app & vbTab & "abbreviation:   " & line.abbreviation & vbTab & "Group Sub Group:   " & line.group & vbTab & "Bank:   " & line.lead & vbTab & "Analyst:   " & line.analyst & vbTab & "at:   " & line.at & vbTab & "Rank:   " & line.rank & vbTab & "Comments:   " & line.comments & vbTab & "Notes:   " & line.notes & vbTab & "Date:   " & line.add & vbTab & "Updated:   " & line.updated & vbTab & "ID:   " & line.id & vbCrLf
        Next
        sBodyEnd = "Best Regards," & vbCrLf & _
                   "Tom"

        sBody = sBodyStart & sBodyInfo & sBodyEnd
        SendEmail emailAdress, "Info", sBody
    Next

End Sub

Sub SendEmail(ByVal sTo As String _
              , ByVal sSubject As String _
               , ByVal sBody As String _
                , Optional ByRef coll As Collection)

    #If Early Then
        Dim ol As Outlook.Application
        Dim outMail As Outlook.MailItem
        Set ol = New Outlook.Application
    #Else
        Dim ol As Object
        Dim outMail As Object
        Set ol = CreateObject("Outlook.Application")
    #End If

    Set outMail = ol.CreateItem(0)

    With outMail
        .To = sTo
        .Subject = sSubject
        .Body = sBody
        .VotingOptions = "Accept;Reject"
        .Importance = 2

        If Not (coll Is Nothing) Then
            Dim item As Variant
            For Each item In coll
                .Attachments.add item
            Next
        End If

        .Display
        .Send
    End With

    Set outMail = Nothing

End Sub

复制数据然后以PDF/图像的形式粘贴特殊内容如何? - Luuklag
2个回答

2

不要设置纯文本Body属性,而是构建一个有效的HTML字符串,并将其赋值给HTMLBody属性。


你能给我一点更多的指导吗?这是我的第一个VBA项目,所以我是个完全的初学者。 - Omar
1
你会HTML吗?如果不会,并且你已经承认自己不懂VBA,那么这将是一个陡峭的学习曲线。也许你应该先学一些VBA的基础知识,然后再学一些HTML的基础知识,然后再往下学习。这个问题没有“按一个按钮”的解决方案。 - user2261597
@Omar,看起来你已经有了基本的HTML表格。这个答案似乎对你创建HTML电子邮件而不是纯文本有一些好的信息。 - FreeMan

1

我没有明显的测试这段代码的方法,所以它可能包含语法错误。我相信我已经提供了足够的解释,让你在必要时修复代码。如果不行,请将出错的语句发布出来,我会诊断原因。

我使用了最简单的HTML。如果你需要更多的格式化,我可以给你一些建议。

一个HTML表格是:<table> ... </table>

一个HTML行是:<tr> ... </tr>

一个HTML单元格是:<td> ... </td>

一个HTML段落是:<p> ... </p>

初始化sBodyStartsBodyEnd

sBodyStart = "<p>Hi, please find your info below:</p>"
sBodyEnd = "<p>Best Regards,<br>Tom</p>"

添加到你的声明中:

Dim CellValue As Variant

将代码中的 sbodyInfo = "" 替换为 Next,修改如下:
sBodyInfo = "<table>"

sBodyInfo = sBodyInfo & "<tr>"
For Each CellValue in Array("Code", "Company Name", "Abbreviation", _
                            "Group Sub Group", "Bank", "Analyst", _
                            "At","Rank","Comments","Notes","Date", _
                            "Updated","ID")
  sBodyInfo = sBodyInfo & "<td>" & CellValue & "</td>"
Next
sBodyInfo = sBodyInfo & "</tr>"

For Each line In colLines

  sBodyInfo = sBodyInfo & "<tr>"
  For Each CellValue in Array(line.app, line.app, line.abbreviation, _
                              line.group, line.lead, line.analyst, _
                              line.at, line.rank, line.comments, _
                              line.notes, line.add, line.updated, line.id)
    sBodyInfo = sBodyInfo & "<td>" & CellValue & "</td>"
  Next
  sBodyInfo = sBodyInfo & "</tr>"

Next

sBodyInfo = sBodyInfo & "</table>"

非常感谢您对此的帮助!在这一部分中,它给我报错了:对于数组(line.app,line.app,line.abbreviation,_ line.group,line.lead,line.analyst,_ line.at,line.rank,line.comments,_ 指出存在编译错误,预期为列表分隔符或)它将“sBodyInfo =”标记为错误。 - Omar
好的,修复了上面的错误,现在又出现了一个错误:"运行时错误 '438':对象不支持此属性或方法" 它正在突出显示相同的部分。对于每个 CellValue 在数组中(line.app、line.version、line.ticker、 line.groupo、line.lead、line.analyst、 line.otw、line.rating、line.watchlist、 line.legal、line.add、line.Update、line.id),请注意。 - Omar
这就是为什么我建议你首先学习基本的VBA。互联网上有很多关于这方面的信息。如果你感兴趣,可以试试这个链接: http://www.homeandlearn.org/ - user2261597
@peakpeak 根据你之前的评论,我确实租了一本《VBA入门指南》来掌握基本概念。然而,与你的建议相反,我与具有VBA/编程知识的人交谈后,他们告诉我这一切都需要实践和指导。我在这里寻求指导。 - Omar
@TonyDallimore 谢谢,我确实回去检查了我的AppInfo模块并纠正了错误。代码运行时没有出现错误,但奇怪的是它没有发送任何电子邮件。你有什么想法吗? - Omar
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接