简体   繁体   English

如何为用 Excel VBA 编写的电子邮件在嵌入 HTML 的图像之间添加间隙或空格?

[英]How to add a gap or space between images embedded into HTML for an E-mail written in Excel VBA?

I am sending an Outlook e-mail from Excel VBA, which picks up 2 Images to display in the Body of the e-mail.我正在从 Excel VBA 发送一封 Outlook 电子邮件,它会选取 2 个图像显示在电子邮件的正文中。 I can successfully get the Images to display next to each other, but I would like a gap or a space between them - how can I add this in?我可以成功地让图像彼此相邻显示,但我希望它们之间有间隙或空间 - 如何添加它?

I don't want them on separate rows.我不希望它们在不同的行中。

I have tried using img tags of "hspace", "border" and "margin" but they don't seem to be recognised or read as they have no effect whatsoever on the display layout of the images.我曾尝试使用“hspace”、“border”和“margin”的 img 标签,但它们似乎无法被识别或读取,因为它们对图像的显示布局没有任何影响。 Or I'm writing them wrong!或者我写错了!

Public Sub EmailData()

Dim OutApp As Object
Dim OutMail As Object
Dim Top10CompaniesPicLocation As String
Dim ReasonsPicLocation As String

Top10CompaniesPicLocation = "\\Images\Top10Companies.jpg"
ReasonsPicLocation = "\\Images\ReasonsTotals.jpg"

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
.To = ""
.Subject = "Performance"
 .Attachments.Add Top10CompaniesPicLocation, 1, 0
 .Attachments.Add ReasonsPicLocation, 1, 0

.HTMLBody = "Good Morning,<br><br>Please see below for snapshots of the Performance.<br><br>"
    
.HTMLBody = .HTMLBody _
& "<img src='cid:Top10Companies.jpg'>" _
& "  " _
& "<img src='cid:ReasonsTotals.jpg'><br><br><br>" _
& "Kind Regards<br><br><br>"

'.Send
.Display

End With

Set OutApp = Nothing
Set OutMail = Nothing

End Sub

In Outlook 2007-2019, hspace , border and margin do not work on <img>在 Outlook 2007-2019 中, hspacebordermargin<img>不起作用

The most consistent way to set a gap of say 20 pixels is to build a table and apply the padding to the td cell.设置 20 像素间距的最一致的方法是构建一个表并将填充应用于td单元格。

Something like this:像这样:

<table role='presentation' cellspacing='0' cellpadding='0' border='0' width='100%'>
  <tr>
    <td style='padding: 10px 10px 10px 0px;'>
        <img src='cid:Top10Companies.jpg'>
    </td>
    <td style='padding: 10px 0px 10px 10px;'>
        <img src='cid:ReasonsTotals.jpg'>
    </td>
  </tr>
</table>

The padding is set to add a little padding on top, bottom, but no padding on the left side of the left image to preserve left alignment with other objects in the email.填充设置为在顶部、底部添加一点填充,但在左侧图像的左侧不添加填充,以保持与电子邮件中其他对象的左对齐。

If you're into brevity, you might be able to make the table simpler by doing this:如果你很简洁,你可以通过这样做使表格更简单:

<table cellspacing='4' width='100%'>
  <tr>
    <td>
      <img src='cid:Top10Companies.jpg'>
    </td>
    <td>
      <img src='cid:ReasonsTotals.jpg'>
    </td>
  </tr>
</table>

That puts an 8px gap between the images, but pushes the left image 4px away from the left margin.这在图像之间放置了 8px 的间隙,但将左侧图像推离了左边距 4px。

Good luck.祝你好运。

Thank you Luis Curado and gwally.谢谢 Luis Curado 和 gwally。 Both of your suggestions work for exactly what I want to achieve - the below code includes both solutions.您的两个建议都适用于我想要实现的目标 - 下面的代码包括这两个解决方案。

Public Sub EmailData()

Dim OutApp As Object
Dim OutMail As Object
Dim Top10CompaniesPicLocation As String
Dim ReasonsPicLocation As String

Top10CompaniesPicLocation = "\\Images\Top10Companies.jpg"
ReasonsPicLocation = "\\Images\ReasonsTotals.jpg"

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
.To = ""
.Subject = "Performance"
.Attachments.Add Top10CompaniesPicLocation, 1, 0
.Attachments.Add ReasonsPicLocation, 1, 0

.HTMLBody = "Good Morning,<br><br>Please see below for snapshots of the Performance.<br><br>"

.HTMLBody = .HTMLBody _
& "<img src='cid:Top10Companies.jpg'>" _
& "&nbsp&nbsp&nbsp&nbsp  " _
& "<img src='cid:ReasonsTotals.jpg'><br><br><br>"

.HTMLBody = .HTMLBody _
& "<table role='presentation' cellspacing='0' cellpadding='0' border='0' width='100%'>" _
& "  <tr>" _
& "    <td style='padding: 10px 10px 10px 0px;'>" _
& "        <img src='cid:Top10Companies.jpg'>" _
& "    </td>" _
& "    <td style='padding: 10px 0px 10px 10px;'>" _
& "        <img src='cid:ReasonsTotals.jpg'>" _
& "    </td>" _
& "  </tr>" _
& "</table>" _
& "Kind Regards<br><br><br>"

'.Send
.Display

End With

Set OutApp = Nothing
Set OutMail = Nothing

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM