在VBA中更改HTML电子邮件正文字体类型和大小

时间:2014-03-05 14:41:52

标签: vba excel-vba outlook-vba excel

我有一个VBA脚本,当在给定的工作表中按下VBA按钮时,该脚本会生成并发送电子邮件。

该脚本目前以相对较小的字体生成电子邮件。我想知道是否有一种方法可以将字体设置为Calibri,文本正好适用于11.

以下是当前的VBA脚本:

Private Sub CommandButton1_Click()

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim strUser As String
Dim signature As String
Dim sTo As String
Dim sCC As String
    'For To field
    Set emailRng = Worksheets("Send Email").Range("D3:I6")

    For Each cl In emailRng
        sTo = sTo & ";" & cl.Value
    Next

    sTo = Mid(sTo, 2)

    'For CC field
    Set emailRngCC = Worksheets("Send Email").Range("D8:I11")

    For Each cl In emailRngCC
        sCC = sCC & ";" & cl.Value
    Next

    sCC = Mid(sCC, 2)


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
    With OutMail
    .Display
    End With
        signature = OutMail.HTMLBody


    strbody = "<FONT SIZE = 3>Good Morning;<p>We have completed our main aliasing process for today.  All assigned firms are complete.  Please feel free to respond with any questions.<p>Thank you."

    With OutMail
        .SentOnBehalfOfName = ""
        .To = sTo
        .CC = sCC
        .BCC = ""
        .Subject = "Data Morning Alias Process - COMPLETE"
        .HTMLBody = strbody & signature
        .Display
    End With
End Sub

我知道这部分代码:

strbody = "<FONT SIZE = 3.5>Good Morning;<p>We have completed our main aliasing

是与电子邮件正文文本大小相关的部分。但是3的设置是小的,4的设置太大。所以我只是想知道是否有某种方法可以将字体设置为大小为11,并将文本格式化为Calibri?

谢谢!

3 个答案:

答案 0 :(得分:18)

我做了一点研究,并且能够编写这段代码:

strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>We have completed our main aliasing process for today.  All assigned firms are complete.  Please feel free to respond with any questions.<p>Thank you.</BODY>"

显然是通过设置"font-size=11pt"而不是设置字体大小<font size=5>, 它允许您像在文本编辑器中一样选择特定的字体大小,而不是像我原来的代码那样选择1-7中的值。

来自simpLE MAn的 link 给了我一些很好的信息。

答案 1 :(得分:0)

仅供参考我也进行了一些研究,如果您要应用的字体系列的名称包含空格(作为示例,我采用 Gill Alt One MT Light ),你应该这样写:

strbody= "<BODY style=" & Chr(34) & "font-family:Gill Alt One MT Light" & Chr(34) & ">" & YOUR_TEXT & "</BODY>"

答案 2 :(得分:0)

设置具有不同大小和样式的文本,以及单元格中文本的大小和样式(带有范围)

Sub EmailManuellAbsenden()

Dim ghApp As Object
Dim ghOldBody As String
Dim ghNewBody As String

Set ghApp = CreateObject("Outlook.Application")
With ghApp.CreateItem(0)
.To = Range("B2")
.CC = Range("B3")
.Subject = Range("B4")
.GetInspector.Display
 ghOldBody = .htmlBody

 ghNewBody = "<font style=""font-family: Calibri; font-size: 11pt;""/font>" & _
 "<font style=""font-family: Arial; font-size: 14pt;"">Arial Text 14</font>" & _
 Range("B5") & "<br>" & _
 Range("B6") & "<br>" & _
 "<font style=""font-family: Chiller; font-size: 21pt;"">Ciller 21</font>" &
 Range("B5")
 .htmlBody = ghNewBody & ghOldBody

 End With

End Sub
'Fill B2 to B6 with some letters for testing
'"<font style=""font-family: Calibri; font-size: 15pt;""/font>" = works for all Range Objekts