使用Excel VBA在Outlook电子邮件中使用HTML设置背景图像 [英] Setting a background image using HTML in an outlook email using Excel VBA

查看:103
本文介绍了使用Excel VBA在Outlook电子邮件中使用HTML设置背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Excel VBA创建一封有关澳大利亚儿童癌症研究所的电子邮件,并附有背景图片(CCIALittleGirl.jpg).

I am trying to use Excel VBA to create an email regarding the Childrens Cancer Institute of Australia with a background image (CCIALittleGirl.jpg).

然后,我希望在文本上或文本框上放置一个理想的文本,上面带有白色粗体文本,可以在运行时填充该文本.

I then want to have text or a text box ideally over it with white bold text that I can populate at runtime.

我可以将图像添加到电子邮件中(在MyHTML部分中注释掉了),但是似乎无法加载背景图像,我正在修改在网上找到的代码,但是我的HTML技能非常接近结束.

I can add an image to the email (the commented out MyHTML part does that) but I can't seem to get a background image to load in, I am modifying code I found online somewhere but my HTML skills are pretty close to nill.

我很确定这是我的问题:

I am pretty sure this part is my issue:

<div style=""background-image: ""cid:CCIALittleGirl.jpg""

这是我到目前为止的代码,MyHTML部分没有达到我的期望.

Here is the code I have so far, the MyHTML part doesn't do what I expect.

Private Sub EmailCopy()
Dim oApp, oMail As Object, MyHTML As String
    Application.ScreenUpdating = False
    On Error Resume Next
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
    'MyHTML = "<p>TempText</p>"
    'MyHTML = MyHTML & "<img src=""cid:CCIALogo.jpg"">"
    MyHTML = "<div style=""background-image: ""cid:CCIALittleGirl.jpg&quot""; height: 390px; width: 900px; color: rgb(0, 0, 0); margin-top: 0px; padding-left: 35px; padding-top: 25px;"">"
    MyHTML = MyHTML & "my text appears here and on top of the image"
    MyHTML = MyHTML & "</div>"
    With oMail
        .To = "Email@Email.com.au"
        .Subject = "TEST"
        .Attachments.Add "C:\Images\CCIA\CCIALogo.jpg"
        .Attachments.Add "C:\Images\CCIA\CCIALittleGirl.jpg"
        .HTMLBody = MyHTML
        .Display
        .Save
        .Close False
    End With
    Application.ScreenUpdating = True
    Set oMail = Nothing
    Set oApp = Nothing
End Sub

感谢您可以提供的任何帮助.

Thanks for any help you can offer.

推荐答案

解决:

MyHTML = "<body background=""cid:CCIALittleGirl.jpg"""

这成功地将图像作为背景.

This successfully puts the image in as a background.

出于完整性考虑(如果其他人有相同的问题),以下是完整的解决方案:

For completeness (in case anyone else has the same question) here is the complete solution:

Private Sub EmailCopy()
Dim oApp, oMail As Object, MyHTML As String, WB As Workbook, FileName As String, BodyText As String, MyText As String
    Application.ScreenUpdating = False
    FileName = ArchiveFolder & ArchiveFileName
    On Error Resume Next
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(0)
    MyText = "Please find attached the CCIA report for " & Format(Now, "DD/MM/YYYY") & "<br><br><br><br><br><br><br><br><br><br>" & _
    "Congratulations to " & StrConv(Sheets("Summary").Range("A2").Text, vbProperCase) & "<br>" & _
    "For an amount of: " & Replace(Sheets("Summary").Range("C2").Text, " ", "") & "<br>" & _
    "Across " & Trim(Sheets("Summary").Range("B2").Text) & " donations."
    MyHTML = "<body background=""cid:CCIALittleGirl.jpg""; center top no-repeat;"
    MyHTML = MyHTML & vbCrLf & "<p style=""font-size:30px;font-weight:Bold;color:rgb(100%,100%,100%)"">" & MyText & "</p>"
    MyHTML = MyHTML & vbCrLf & "<br><br><br><br><img src=""cid:CCIALogo.jpg"">"
    With oMail
        .to = "email@email.com"
        .Subject = "CCIA report @ " & Format(Now, "DD/MM/YYYY")
        .Attachments.Add "C:\Images\CCIA\CCIALogo.jpg"
        .Attachments.Add "C:\Images\CCIA\CCIALittleGirl.jpg"
        .Attachments.Add FileName & ".xlsx"
        .HTMLBody = MyHTML
        .Display
        .Save
        .Close False
    End With
    Application.ScreenUpdating = True
    Set oMail = Nothing
    Set oApp = Nothing
End Sub

这篇关于使用Excel VBA在Outlook电子邮件中使用HTML设置背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆