将HTML文件加载到VBA Microsoft Access电子邮件中

时间:2015-05-21 22:06:51

标签: html vba outlook access-vba

我正在尝试将HTML文件加载到由我的Microsoft Access数据库发送的电子邮件中。当用户点击按钮时,将发送电子邮件(Command109)

这是我发送电子邮件的代码:

Private Sub Command109_Click()
'Start of code
Dim strEmail, strBody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

'Creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.Application")

DoEvents

Set objEmail = objOutlook.CreateItem(olMailItem)

DoEvents

'Creates string with email address
strEmail = PayeeEmail
strBody = "WHAT SHOULD I PUT HERE TO LOAD AN EXTERNAL HTML FILE?"

DoEvents

'Creates and sends email
With objEmail

DoEvents
.To = strEmail

DoEvents
.Subject = "Your Distribution from " & COMPANY & " has been processed."

DoEvents
.HTMLBody = strBody
DoEvents
DoEvents
.Send
End With

Set objEmail = Nothing
'Closes Outlook. Remove if you do not want to close Outlook
'objOutlook.Quit
Exit Sub
End Sub

我有其他代码允许我将HTML文件加载到Outlook中,但我不确定如何组合代码 - 以便将HTML文件加载到Access发送的电子邮件的BODY中

以下是我将用于将HTML文件加载到Outlook中的宏的代码:

Sub insertHTML()
Dim insp As Inspector
Set insp = ActiveInspector
If insp.IsWordMail Then
Dim wordDoc As Word.Document
Set wordDoc = insp.WordEditor
wordDoc.Application.Selection.InsertFile "C:\Users\me\Desktop\emailtemplate.html",
, False, False, False
End If
End Sub

任何人都可以帮我解决这个问题吗?谢谢你的时间!

2 个答案:

答案 0 :(得分:0)

我不确定是否直接导入HTML文件,但过去我只是将HTML代码直接放入模块中。这是可能的,因为您使用.HTMLBody而不是.Body。您也可以通过这种方式将变量插入HTML代码中。

直接HTML字符串

strBody = "<html> YOUR HTML CODE HERE </html>"

使用VBA变量的HTML

strBody = "<html><p> This is an email from " & COMPANY & ". We value your business</p></html>"

显然,如果模板经常更改,这并不理想。在我过去完成此操作时,我只是在outlook中创建了一个模板,将HTML代码复制到VBA中,然后将变量插入到我想要的位置。

虽然可能有更好的方法。

答案 1 :(得分:0)

要在多行上连接字符串,您必须在'&amp;'之间留一个空格和'_',因为他们是独立的运营商。

strBody = "<html><p> some of my html text here" & _  'Note the spaces
          "more formatted html text here" & _
          "even more formatted html text here" & _
          "don't forget your closing html brackets</p></html>"

with objEmail
    .to = strTo
    .subject = strSubject
    .HTMKBody = strBody
    .send
end with

为了便于阅读代码,您甚至可以使用电子邮件字符串创建一个单独的模块,只需确保它是公开的,以便可以调用它。

public strBody as string = your string here