【发布时间】:2017-04-07 05:56:59
【问题描述】:
这是我从几个不同站点拼凑而成的自动邮件合并。
为了确保发送的电子邮件是 HTML 并包含默认用户签名,它已被多次更改。
点击按钮后,弹出一个窗口选择范围,然后根据范围选择个性化电子邮件。
Sub EmailAttachmentRecipients()
'updateby Extendoffice 20160506
Dim xOutlook As Object
Dim xMailItem As Object
Dim xRg As Range
Dim xCell As Range
Dim xTxt As String
Dim Signature As String
Dim xEmail As String
Dim xSubj As String
Dim xMsg As String
Dim i As Integer
Dim k As Double
' Create window to select range
xTxt = ActiveWindow.RangeSelection.address
Set xRg = Application.InputBox("Please select the arresses list:", "Water Corporation Mail Merge", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
For i = 1 To xRg.rows.Count
Set xOutlook = CreateObject("Outlook.Application")
Set xMailItem = xOutlook.CreateItem(0)
xMsg = "<BODY style=font-size:11pt;font-family:Verdana>" & Sheet2.Cells(4, 2) & " " & xRg.Cells(i, 1) & ",</BODY>" & "<br>"
xMsg = xMsg & "" & Sheet2.Cells(6, 2) & " " & Sheet2.Cells(6, 4) & " " & Sheet2.Cells(6, 6) & " " & "<br>" & "<br>"
xMsg = xMsg & "" & Sheet2.Cells(8, 2) & " " & "<br>" & "<br>"
xMsg = xMsg & "" & Sheet2.Cells(10, 2) & "" & "<br>"
xEmail = xRg.Cells(i, 2)
With xMailItem
.Display
.To = xEmail
.CC = ""
.Subject = "" & Sheet2.Cells(2, 2) & " " & xRg.Cells(i, 3) & " - " & xRg.Cells(i, 4) & ""
.HTMLBody = xMsg & .HTMLBody
.Send
End With
Set xOutlook = Nothing
Set xMailItem = Nothing
Next i
End Sub
我无法获取发送多封电子邮件的代码,因为选择了 5 行的范围,而电子邮件客户端只发送一封电子邮件。
有没有人可以出借的方向?
感谢大家解决这个问题!
【问题讨论】:
-
为什么你的 for 循环里面什么都没有?
-
对不起,伙计们,在 Sven Ras 的回答之后,刚刚根据问题中的上述代码重新编辑,现在似乎工作得更好了,但是现在当我发送给多个以下电子邮件时,每个电子邮件都包含来自最后一封电子邮件。正如电子邮件 1 中所说,你好 Sven。电子邮件 2 说你好,斯文,你好约翰。电子邮件 3 天 Hello Sven、Hello John、Hello Peter。
-
在循环末尾添加 set xoutlook =nothing set xmailitem=nothing
-
或者您可以在循环外创建 Outlook 对象,并在循环结束后将其设置为空。
-
谢谢 Sivar,我按照上面的方法试过了吗?似乎没有用,它是否正确包含?
标签: html vba excel html-email