将 HTML 文件加载到 VBA Microsoft Access 电子邮件
Load HTML file into VBA Microsoft Access Email
我正在尝试将 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 发送的电子邮件正文。
这是我的宏代码,它将 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
谁能帮我解决这个问题?感谢您的宝贵时间!
我不确定是否要直接导入 HTML 文件,但过去我只是将 HTML 代码直接放入模块中。这是可能的,因为您使用的是 .HTMLBody 而不是 .Body。您也可以通过这种方式将变量插入 HTML 代码中。
直 HTML 字符串
strBody = "<html> YOUR HTML CODE HERE </html>"
HTML 使用 VBA 个变量
strBody = "<html><p> This is an email from " & COMPANY & ". We value your business</p></html>"
显然,如果模板经常更改,这并不理想。当我过去这样做时,我只是在 outlook 中制作了一个模板,将 HTML 代码复制到 VBA 中,然后在我想要的地方插入变量。
虽然可能有更好的方法来做到这一点。
要连接多行字符串,您必须在“&”和“_”之间添加一个 space,因为它们是单独的运算符。
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 以便可以调用它。
public strBody as string = your string here
我正在尝试将 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 发送的电子邮件正文。
这是我的宏代码,它将 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
谁能帮我解决这个问题?感谢您的宝贵时间!
我不确定是否要直接导入 HTML 文件,但过去我只是将 HTML 代码直接放入模块中。这是可能的,因为您使用的是 .HTMLBody 而不是 .Body。您也可以通过这种方式将变量插入 HTML 代码中。
直 HTML 字符串
strBody = "<html> YOUR HTML CODE HERE </html>"
HTML 使用 VBA 个变量
strBody = "<html><p> This is an email from " & COMPANY & ". We value your business</p></html>"
显然,如果模板经常更改,这并不理想。当我过去这样做时,我只是在 outlook 中制作了一个模板,将 HTML 代码复制到 VBA 中,然后在我想要的地方插入变量。
虽然可能有更好的方法来做到这一点。
要连接多行字符串,您必须在“&”和“_”之间添加一个 space,因为它们是单独的运算符。
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 以便可以调用它。
public strBody as string = your string here