VBA 使用 CDO 发送格式化的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23542872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Sending formatted email with CDO
提问by DA69
I have this code, that works.
我有这个代码,它有效。
Private Sub CommandButton1_Click()
Dim Mail As New Message
Dim Config As Configuration
Set Config = Mail.Configuration
Dim cell As Range
Dim strbody As String
For Each cell In Sheets("Sheet1").Range("C2:D9")
strbody = strbody & cell.Value & vbNewLine
Next
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 25
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = "MyEmail"
Config(cdoSendPassword) = "EmailPassword"
Config.Fields.Update
Mail.To = Sheets("Sheet1").Range("j2").Value
Mail.From = Config(cdoSendUserName)
Mail.Subject = "Email Subject"
Mail.TextBody = strbody
If G9 = "Yes" Then
Mail.AddAttachment "C:\...file1.xls"
Mail.AddAttachment "C:\...file2.xls"
End If
On Error Resume Next
Mail.Send
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "There was an error"
Exit Sub
End If
MsgBox "Your email has been send!", vbInformation, "Sent"
End Sub
It will take the text I have in C2:D9 but it does not format it as it is in the sheet, so it looks like this when sent:
Item1
Result1
Item2
Result2
Item3
Result3
Item4
Result4
它将采用我在 C2:D9 中的文本,但它没有按照工作表中的格式对其进行格式化,因此发送时看起来像这样: Item1
Result1
Item2
Result2
Item3
Result3
Item4
Result4
If I set the Mail.TextBody to Mail.HTMLBody, it comes out like this:
如果我将 Mail.TextBody 设置为 Mail.HTMLBody,它会是这样的:
Item1 Result1 Item2 Result2 Item3 Result3 Item4 Result4
项目1 结果1 项目2 结果2 项目3 结果3 项目4 结果4
How can I make it send exactly what is in those ranges as it is formatted in the excel sheet? If I were to manually copy those cells and paste them into an email body, it would keep the cell structure, font size, font color, and any background colors as well.
我怎样才能让它准确地发送这些范围内的内容,因为它在 Excel 工作表中被格式化?如果我手动复制这些单元格并将它们粘贴到电子邮件正文中,它将保留单元格结构、字体大小、字体颜色和任何背景颜色。
Item1 Result1
Item2 Result2
Item3 Result3
Item4 Result4
项目1 结果1
项目2结果2 项目
3结果3项目4
结果4
I have been looking online a lot to try and find this but have not been able to for CDO. I found some items for Outlook but I am not using Outlook.
我一直在网上寻找很多尝试找到它,但一直无法找到 CDO。我找到了一些 Outlook 项目,但我没有使用 Outlook。
Is it possible to send a formatted block of cells using the CDO email method of sending an email?
是否可以使用发送电子邮件的 CDO 电子邮件方法发送格式化的单元格块?
Thank you.
谢谢你。
回答by whytheq
An example of using HTML formatted mail is as follows. You need to include the correct tags:
使用 HTML 格式邮件的示例如下。您需要包含正确的标签:
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
fEmailTOSend = RepEmailTO
.To = "[email protected]"
.Subject = "report created " & strdate
.BodyFormat = 2 '<<HTML
.importance = 2 '<<High
.HTMLBody = "<P>" & _
"<BR>" & _
"<FONT face=""Lucida Sans Unicode"" size=2.5>" & _
myEmailNarrative & _
"<BR>" & _
"Kind Regards," & _
"<BR>" & _
"<FONT color=#0000ff>" & _
ProfileName & "." & _
"</FONT>" & _
"</FONT>" & _
"</P>" & _
"</BODY></HTML>"
.Attachments.Add wb.FullName
.Save
.Send
End With