从 VBA 电子邮件程序发送 html 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29418891/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:46:12  来源:igfitidea点击:

Sending html email from VBA email program

htmlvba

提问by jpl458

I have written an email program for my organization the handles some very specialized things very well, things I could use Outlook or Gmail for. Now, the manager would like to send an occasional email to our small customer base, but I want the email body tto look professional and not send it as an attachment. I have cobbled together an html document that present in all browsers and has been validated. My problem is I can't figure out how to point the message body at the html document. Here is the salient code.

我为我的组织编写了一个电子邮件程序,它可以很好地处理一些非常专业的事情,我可以使用 Outlook 或 Gmail 来处理这些事情。现在,经理想偶尔向我们的小客户群发送一封电子邮件,但我希望电子邮件正文看起来很专业,而不是作为附件发送。我拼凑了一个出现在所有浏览器中并经过验证的 html 文档。我的问题是我不知道如何将消息正文指向 html 文档。这是突出的代码。

This is where all is set up:

这是所有设置的地方:

Do While mailRs.EOF = False
'Me.AttachDoc = "C:\EmailFolder\CouponForm.pdf"
  emTo = mailRs.Fields("EmailAddr").Value
  emFrom = "[email protected]"
  emSubject = Me.Subject
  emtextBody = Me.TextMessage

Here is a the call for sending the email

这是发送电子邮件的电话

Call SendAMessage(emFrom, mailRs.Fields("EmailAddr").Value, _
                   emSubject, emtextBody, emAttach)

(I got the code for sending the email off the web and it works great through our mail server.)

(我得到了从网络发送电子邮件的代码,它通过我们的邮件服务器运行良好。)

In the above, before the call @ emtextBody = Me.TextMessageis where I need to replace Me.TextMessagewith the address/body of the html document. And the message box is a textBox on the ACCESS form. I can't find any control in ACCESS that takes html. I can't use the path to the html document because that generates an error. Is there a way of getting around this

在上面,在调用 @ 之前emtextBody = Me.TextMessage,我需要Me.TextMessage用 html 文档的地址/正文替换。消息框是 ACCESS 表单上的文本框。我在 ACCESS 中找不到任何需要 html 的控件。我无法使用 html 文档的路径,因为这会产生错误。有没有办法解决这个问题

If more information is required I'll be happy to supply it.

如果需要更多信息,我很乐意提供。

Thanks for your time.

谢谢你的时间。

jpl

喷气机

回答by jbay

Use something like the below code. I've included elements for attachment as well as html formatting but pretty much anything you can write in html can also be done within vba.

使用类似下面的代码。我已经包含了用于附件和 html 格式的元素,但是几乎所有可以用 html 编写的内容也可以在 vba 中完成。

Sub SharePerformance()

Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.createitem(0)
'& "<a href=""\server\folder"">\server\folder</a>" &
msg1 = "Team,<br><br><b><DL>" & Range("b5").Value & "</b><br><ul><b><u>" & Range("b6").Value & "</b></u>"
msg1 = msg1 & "<DT><a HREF=C:\USER\Desktop\File1.xlsb>"
msg1 = msg1 & Range("b7").Value & "</a><br>"
msg1 = msg1 & "<b><u>" & Range("b9").Value & "</b></u></DL><br><br>"


msg1 = msg1 & "<p><img src=file://" & "C:\temp\Chart1.png" & "></p>" & "<br>"

On Error Resume Next
' Change the mail address and subject in the macro before you run it.

With OutMail
    .To = Range("B1").Value
    .cc = ""
    .BCC = ""
    .Subject = Range("B3").Value
    .HTMLBody = msg1
    '.Attachments.Add ActiveWorkbook.FullName
    '.Attachments.Add ("C:\temp\Chart1.png")
    '.Attachments.Add ("C:\temp\Chart2.png")
    .display
End With
SendKeys "^{ENTER}"
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

回答by nvuono

I can't tell what code is inside that SendAMessagefunction you are using but all the VBA examples I've worked with seem to work the same way with the CDO.Message object like in this MS Knowledge Base article KB286431. At some point SendAMessageis going to have a line that assigns the message object's .TextBodyvalue to be equal to the emtextBodyparameter you pass in.

我不知道SendAMessage您正在使用的函数中包含哪些代码,但我使用过的所有 VBA 示例似乎都以与 CDO.Message 对象相同的方式工作,如 MS 知识库文章KB286431 中所示。在某些SendAMessage时候,会有一行将消息对象的.TextBody值分配为等于emtextBody您传入的参数。

One solution may be to copy your SendAMessage function into a new function SendAMessageHTML and replace the line where they are setting someMessage.TextBody = emtextBodyso that you are setting someMessage.HTMLBody = emtextBody

一种解决方案可能是将您的 SendAMessage 函数复制到一个新函数 SendAMessageHTML 并替换它们设置的行,someMessage.TextBody = emtextBody以便您设置someMessage.HTMLBody = emtextBody

Assuming your textbox has text along the lines of "<html><head><body></body></html>"you could modify your existing function to do a naive check like this:

假设你的文本框有文本,"<html><head><body></body></html>"你可以修改你现有的函数来做一个简单的检查,如下所示:

if Left(UCase(emtextBody),6) = "<HTML>" then
  someMessage.HTMLBody = emtextBody
else
  someMessage.TextBody = emtextBody
end if