我有一个宏,允许我向每个经理发送每月绩效的邮箱.代码如下:

Sub OutlookEmailsSend()
    
    Dim objOutlook As Outlook.Application
    Dim objMail As Outlook.MailItem
    Dim lCounter As Long
    Dim endColumnNo As Long
    Dim a As Long
    Dim sFile As String
    
    endColumnNo = ThisWorkbook.Sheets("Sheet1").UsedRange.Columns.Count
    
    Set objOutlook = Outlook.Application
   
    For lCounter = 2 To 3
        '
        Set objMail = objOutlook.CreateItem(olMailItem)
        
        objMail.To = Sheet1.Range("B" & lCounter).Value
              
        objMail.Subject = "Sales Summary"
        
        sFile = "Dear,<br><br>Please refer to below table for your performance<br><br><table border=1>"
        
        For a = 1 To endColumnNo
            sFile = sFile & "<tr><td>" & Cells(1, a) & "</td><td>" & Cells(lCounter, a) & "</td></tr>"
        Next
        
        objMail.HTMLBody = sFile
        
        objMail.Display
       
        Set objMail = Nothing
    Next
    
End Sub

宏生成如下表

Dear,

Please refer to below table for your performance

Name    Tom
Email   sgcjack@163.com
Item    Phone
Sales   123
Bonus   3213

然而,我希望餐桌上的礼物如下

Name    Email                   Item     Sales  Bonus
Jack    jacksun@citics.com.hk   Computer 342    23123

有什么办法可以做到这一点吗?

推荐答案

为了更好的可读性,在函数中组织html创建并将函数结果分配给objMail.HTMLBody(省略循环)可能会有所帮助.

Btw you forgot the closing table tag 100 which wouldn't result in a valid html structure. - Of course, the most direct approach following the original code would be to follow the recommendation in comment to add the 101 tags outside the loop not forgetting the closing 100 tag.

    With Sheet1
        objMail.HTMLBody = getBody(.Range("A1",.Cells(1,EndColumnNo)),"Dear xx")`
    End With

help function 100基于(c)明确定义的表 struct 连接(a)头和(b)表数据.

Note: You can play around and change that definition to a more sophisticated html code with separate header tags, too..

Function getBody(rng    As Range, _
    Optional greetings  As String = "", _
    Optional HeaderList As String = "Name,Email,Item,Sales,Bonus")
Const Blanks As String = "  "
'a) get headers
    Dim headers As String
    headers = "  <td>" & Replace(HeaderList, ",", "</td><td>") & "</td>"
'b) join table data "<td>..</td>"
    Dim data As String
    data = Blanks & _
        Join(rng.Parent.Evaluate("""<td>""&" & rng.Address(0, 0) & " & ""</td>""") _
        , vbNewLine & Blanks)
'c) define table structure
    Dim tags()
    tags = Array(greetings, _
        "<table border='1'>", _
        " <tr>", headers, " </tr>", _
        " <tr>", data, " </tr>", _
        "</table>")
'd) return joined function result
    getBody = Join(tags, vbNewLine)
End Function

Html相关问答推荐

HTML CSS如何使用图像作为中心点将元素置于中心

NG8004:找不到名为""的管道.'' [插件Angular 编译器]

将ANGLE模板高效地迁移到ANGLE 17中引入的新语法

我应该怎么做才能显示出整个字符串?

有没有办法根据另一个项目调整FlexBox项目的大小?

如何在网格中拥有离散的行

如何解决水平塌陷问题?

静态DIV的标签,显示从窗体输入的值

当底部进入视图时从底部粘性定位

是否有语义 HTML 可以澄清含义?

perl hdb 调试器:浏览器以错误的编码显示 UTF-8 源代码

:after 伪元素没有出现,即使它有 content 属性

用由文本制成的边框包围内容

为什么 CSS 网格超出了父级?

网页设计不适合移动设备

我正在使用 NVDA 并多次读取关闭按钮,但它的读取非常完美

屏幕缩小时背景图像裁剪高度

HTML、CSS设计图像出格

无法从社交栏中 Select 选项

如何将第一个 p 标签放在其下方第二个 p 标签的中间