如何通过Excel VBA代码更改通过Outlook发送的电子邮件的字体格式?

我有VBA代码,使用Excel通过Outlook发送一些电子邮件。

它将每个联系人的姓名input到电子邮件正文(亲爱的阿-),将公司名称/发票号码input主题行(公司ABC发票123),为每个联系人附上不同的文件(发票123.pdf ),将邮件标记为高度重要的,请求读取的收据,代表用于发票的辅助电子邮件帐户而不是我的个人电子邮件帐户,将BCC发送到辅助电子邮件,并且在底部添加电子邮件签名以图片。

大部分的代码(如果不是全部的话)来自这个美好的Ron de Bruin网站 。

我粘贴了很多代码来获取我的位置,不知道我是如何设法使其工作的。

我尝试了许多不同的“标签”,试图改变字体格式。

我试着改变Outlook的模板设置,并把代码输出,希望它会input“Outlook默认”字体。

我的代码中有样式标签,但没有太多的效果。 它被设置为Times New Roman Size 18。

作为Times New Roman Size 12的“Dear So-and-so”input。电子邮件正文input为Calibri 13.5。 我的签名input,无论我设置在签名设置。

我希望它是相同的字体,大小,颜色等

Sub Mail_Outlook_With_Signature_Html_1() ' Working in Office 2000-2013 Dim OutApp As Object Dim OutMail As Object Dim sh As Worksheet Dim strbody As String Dim cell As Range Dim FileCell As Range Dim rng As Range With Application .EnableEvents = False .ScreenUpdating = False End With Set sh = Sheets("Sheet1") Set OutApp = CreateObject("Outlook.Application") For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants) Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1") If cell.Value Like "?*@?*.?*" And _ Application.WorksheetFunction.CountA(rng) > 0 Then Set OutMail = OutApp.CreateItem(0) strbody = "<P STYLE='font-family:Times New Roman;font-size:18'>This is the message text for each message. This is all the same for each contact!</P>" On Error Resume Next With OutMail .Display .To = cell.Value .CC = "" .BCC = "MyCompanyEmail@company.com" .Subject = Cells(cell.Row, "D").Value .HTMLBody = "Dear " & Cells(cell.Row, "A").Value & "," & "<br>" & "<br>" & strbody & .HTMLBody .Importance = 2 .ReadReceiptRequested = True .SentOnBehalfOfName = """My Company Email Name"" <MyCompanyEmail@company.com>" For Each FileCell In rng.SpecialCells(xlCellTypeConstants) If Trim(FileCell) <> "" Then If Dir(FileCell.Value) <> "" Then .Attachments.Add FileCell.Value End If End If Next FileCell .Display End With On Error GoTo 0 Set OutMail = Nothing End If Next cell Set OutApp = Nothing With Application .EnableEvents = True .ScreenUpdating = True End With End Sub 

编辑:罗兰邵注意到,字体大小没有任何澄清,什么是18。 18pts? 18像素? 18英寸? 所以我改变了下面的代码。

 strbody = "<P STYLE='font-family:Times New Roman;font-size:18pt'> 

这使得文本正文的格式更改,但并没有改变电子邮件的“亲爱的何谓”部分。 仍然希望有一些改变。

第二编辑 :h4xpacebuild议看看这篇文章的一些指导。 我已经试图在发布之前遵循本指南,但是我无法使其工作。 我回去尝试添加我所想的是改变主体字体所需的相关代码。 以下代码的添加和更改:

  With OutMail .Display .To = cell.Value .CC = "" .BCC = "MyCompanyEmail@company.com" .Subject = Cells(cell.Row, "D").Value **.BodyFormat = olFormatHTML** .HTMLBody = **"<HTML><BODY><b>"**Dear " & Cells(cell.Row, "A").Value & "," & "<br>" & "<br>" & strbody & .HTMLBody **</b></BODY></HTML>"** .Importance = 2 .ReadReceiptRequested = True .SentOnBehalfOfName = """My Company Email Name"" <MyCompanyEmail@company.com>" 

这也没有工作。

我不太了解VBA添加必要的代码段而不改变我已经有的东西。

最终的变化

代码正如我最初打算的那样工作。 两个主要的变化是以下几行。 在strbody上,我需要将“pt”添加到字体大小。 另外,我需要将格式信息放在任何“消息内容”之前。

 strbody = "<P STYLE='font-family:TimesNewRoman;font-size:12.5pt;color: rgb(31,73,125)'>This is the message text for each message. This is all the same for each contact!</P>" .HTMLBody = "<P STYLE='font-family:TimesNewRoman;font-size:12.5pt;color: rgb(31,73,125)'>Dear " & Cells(cell.Row, "A").Value & "," & "<br>" & "<br>" & strbody & .HTMLBody 

你几乎在那里,你正在创build你的电子邮件正文为

 .HTMLbody = 'SomeText' + strbody(Formatting + MoreText) 

由于格式部分是通过它的一部分,它只影响后面的文本。 Make .HTMLBody从你的格式化信息开始,接下来的所有内容都会把它提取出来。

您需要将OutMail对象的.BodyFormat属性设置为olFormatHTML。

请参阅此处提供的示例: http : //msdn.microsoft.com/en-us/library/office/aa171418(v=office.11​​).aspx

我认为,OP和我试图解决同样的问题,以发送正常格式的消息,并通过ron debruin的方法保持签名。

我解决了我的问题,join:

 .BodyFormat = olFormatHTML .HTMLBody = "<HTML><BODY>" & strbody & "</BODY></HTML>" & .HTMLBody 

到.OutMail对象

对我有用的是:

 .HTMLBody = strbody & TextLine1 & vbcrlf & strbody & TextLine2 

即将格式化string放在每行文本之前。