【问题标题】:Change HTML email body font type and size in VBA在 VBA 中更改 HTML 电子邮件正文字体类型和大小
【发布时间】:2020-06-21 07:26:38
【问题描述】:

我有一个 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?

谢谢!

【问题讨论】:

  • 如果是 HTML,它可能是这样的:&lt;p&gt;&lt;font size="5" font face="calibri" color="blue"&gt;This is some text!&lt;/font&gt;&lt;/p&gt;。我是从This site 那里得到的。虽然 ”。”似乎没有考虑到所以font size="5" = font size="5.1" = font size="5.9"
  • 也许这就是你想要的,来自Same site:&lt;p style="font-size:11px"&gt;This is some text!&lt;/p&gt;
  • 感谢您的帮助,我查看了您发布的网站,它为我提供了在 Google 上搜索内容的基础。

标签: excel vba outlook


【解决方案1】:

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

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"而不是设置字体大小&lt;font size=5&gt;, 它允许您像通常在文本编辑器中一样选择特定的字体大小,而不是像我最初的代码那样从 1-7 中选择一个值。

这个来自simple MAn的link给了我一些很好的信息。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 2017-03-14
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        • 2022-01-01
        • 2014-02-01
        • 1970-01-01
        • 2015-05-30
        • 1970-01-01
        相关资源
        最近更新 更多