简体   繁体   English

VBA Excel电子邮件格式问题

[英]VBA Excel Email Formatting Issue

My data is set up like this: 我的数据是这样设置的:

-On Excel, I have a worksheet, "Test1", which has a list of people I want to send emails to. -在Excel上,我有一个工作表“ Test1”,其中包含要向其发送电子邮件的人员列表。 Like such; 像这样

No.    Version  Company  Name    Email
 1       R         x     Max     max@abc.com
 2       E         y     Bill    bill@abc.com
 3       C         z     Scott   scott@abc.com

The emails I want to send differ slightly by type. 我要发送的电子邮件在类型上略有不同。 My current code is as follows: 我当前的代码如下:

Sub mailTest()

Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem

For i = 2 To 3

   Set olApp = New Outlook.Application
   Set olMail = olApp.CreateItem(olMailItem)

    mailBody = Worksheets("BODY").Cells(1, 1).Value & Worksheets("Test1").Cells(i, 4) _
 & Worksheets("BODY").Cells(2, 1).Value



   With olMail
       .To = Worksheets("Test1").Cells(i, 5).Value
       .Subject = Worksheets("Test1").Cells(i, 3).Value
       .HTMLBody = mailBody
       .Display

End With


   Set olMail = Nothing
   Set olApp = Nothing

Next



End Sub

The code above displays the emails as the main body is in Cell A2 of sheet "BODY". 上面的代码显示电子邮件,因为主体位于工作表“ BODY”的单元格A2中。 The "Hello" is stored in Cell A1, and also displays fine as I have HTML coding in both these cells to set the font to Calibri and the size to 3. “ Hello”存储在单元格A1中,并且由于在这两个单元格中都有HTML编码(将字体设置为Calibri且大小设置为3),因此显示也很好。

The issue is when I'm pulling the names of people from sheet Test1 in Column 4 using the code; 问题是当我使用代码从第4列的工作表Test1中提取人员姓名时;

 & Worksheets("Test1").Cells(i, 4)

It is displaying with a different font, defaulting to Times New Roman. 它以其他字体显示,默认为Times New Roman。 What I'm left with is everything displaying in Calibri, with just their names being pulled in Times New Roman. 我剩下的是Calibri中显示的所有内容,而在Times New Roman中只是拉了他们的名字。

Is there any way to fix this without adding HTML code in each of the name cells? 有什么方法可以解决此问题,而无需在每个名称单元中添加HTML代码? The actual file I'm working on has about 500 names so manually doing it would be a pain... 我正在处理的实际文件大约有500个名称,因此手动执行操作会很麻烦...

Thanks everyone. 感谢大家。

Try replace 尝试更换

.HTMLBody = mailBody

with

.HTMLBody = "<!DOCTYPE html><html><head><style>"
.HTMLBody = .HTMLBody & "body{font-family: Calibri, ""Times New Roman"", sans-serif}"
.HTMLBody = .HTMLBody & "</style></head><body>"    
.HTMLBody = .HTMLBody & mailBody & "</body></html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM